#include
#include
#include
#define LL long long
#define maxn 100009
using namespace std;
int n;
LL m[maxn],r[maxn];
LL exgcd(LL a,LL b,LL &x,LL &y){
if(b==0){
x=1;y=0;
return a;
}
LL t,r=exgcd(b,a%b,x,y);
t=x;x=y;y=t-a/b*y;
return r;
}
LL CRT(LL m[],LL r[],LL n){
LL x,y,gcd,M=m[1],R=r[1];
for(int i=2;i<=n;i++){
LL gcd=exgcd(M,m[i],x,y);
if((r[i]-R)%gcd)return -1;
x=(r[i]-R)/gcd*x%(m[i]/gcd);
R+=x*M;//新的余数R=r1+x*m[1]
M=M/gcd*m[i];//新的M,lcm(m1,m2)
R%=M;
}
return R>0?R:R+M;
}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)
scanf("%lld%lld",&m[i],&r[i]);
printf("%lld\n",CRT(m,r,n));
}
return 0;
}