include[HTML_REMOVED]
using namespace std;
define int long long
const int N = 1e5 + 10;
int n, a[N], b[N];
int exgcd(int a, int b, int &x, int &y) {
if(!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
int msc(int a, int b, int p) {
int ans = 0;
while(b) {
if(b & 1) ans = (ans + a) % p;
b >>= 1;
a = (a + a) % p;
}
return ans;
}
int excrt() {
int M = b[1], ans = a[1];
for(int i = 2; i <= n; i++) {
int x, y;
int c = ((a[i] - ans) % b[i] + b[i]) % b[i];
int g = exgcd(M, b[i], x, y);
if(c % g) return -1;
int t = b[i] / g;
x = msc(x, c / g, t);
ans += M * x;
M *= t;
ans = (ans % M + M) % M;
}
return ans;
}
signed main() {
cin >> n;
for(int i = 1; i <= n; i++) cin >> b[i] >> a[i]; //b是模数
cout << excrt();
return 0;
}