inlineintread(){ int x = 0, f = 0; char c = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? -x : x; }
#define N 100010
int n, a[N], b[N], P;
intMul(int x, int k, int r = 0){ for (; k; k >>= 1, x = (x + x) % P) { if (k & 1) r = (r + x) % P; } return r; }
intgcd(int a, int b){ return !b ? a : gcd(b, a % b); }
voidexgcd(int a, int b, int &x, int &y){ if (b == 0) {x = 1, y = 0; return;}; exgcd(b, a % b, x, y); int t = y; y = x - (a / b) * y, x = t; }
intexcrt(){ int A = a[1], B = b[1], k, y; // A : a1, B : b1, k : k1 for (int i = 2; i <= n; i ++) { // a[i] : a2, b[i] : b2 int d = gcd(B, b[i]), t = (a[i] - A % b[i] + b[i]) % b[i]; // t : a2 - a1, 在模 b2 意义下 exgcd(B, b[i], k, y); if (t % d) return-1; P = b[i] / d; A += Mul(k, t / d) * B; // a = k1b1 + a1 B = B / d * b[i]; // b = lcm(b1, b2) A = (A + B) % B; } return A; }
signedmain(){ n = read(); for (int i = 1; i <= n; i ++) { b[i] = read(), a[i] = read(); } printf("%lld\n", excrt()); return0; }