#include <iostream>
using namespace std;
const int N = 25 + 5; //remember to modify the range of the data!!
typedef long long ll;
ll n, m, t;
ll a[N], b[N], c[N];
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if(b == 0)
{
x = 1, y = 0;
return a;
}
ll g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
ll mul(ll a, ll p, ll mod)
{
ll res = 0;
while(p > 0)
{
if(p & 1)
res = (res + a) % mod;
a = (a + a) % mod;
p >>= 1;
}
return res;
}
int main(void)
{
ll x, y;
cin >> n;
m = 1;
cin >> a[1] >> b[1];
for(int i = 2; i <= n; i++)
{
cin >> a[i] >> b[i]; //a[i] 是模数, b[i]是模后的余数
ll g = exgcd(a[i - 1], a[i], x, y);
if((b[i] - b[i - 1]) % g != 0) //无解不能合成一个方程
{
cout << -1 << '\n';
return 0;
}
ll k = (b[i] - b[i - 1]) / g, mod = a[i] / g;
x = (x % mod + mod) % mod; //保证x 是正数
x = (mul(k, x, mod) + mod) % mod;
a[i] = a[i - 1] * a[i] / g; //合成后的模数是最小公倍数
b[i] = (mul(a[i - 1], x, a[i]) + b[i - 1]) % a[i];// 合成后的 x*
}
cout << b[n] << '\n';
return 0;
}