#include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[500],r[500],k; ll gcd(ll a,ll b){ while(b!=0){ ll t=a; a=b; b=t%b; } return a; } ll lcm(ll a,ll b){ return a*b/gcd(a,b); } ll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0) {x=1;y=0;return a;} ll r=exgcd(b,a%b,x,y); ll t=x;x=y;y=t-a/b*y; return r; } ll inv(ll a,ll p){//a模p意义下的逆 ll d,x,y;d=exgcd(a,p,x,y); return (x+p)%p==0?p:(x+p)%p; } ll CRT(ll a[],ll m[],ll n){ ll M=1; ll ans=0; for(int i=1;i<=n;i++) M*=m[i]; //M为Πmi //并设Mi=M/mi,ti=Mi^-1表示Mi模mi意义下的倒数 //即Miti≡1%mi; for(int i=1;i<=n;i++) { ll x,y; x=inv(M/m[i],m[i]); ans=(ans+M/m[i]*x*m[i])%M; } ans=(ans+M)%M; return ans; } inline ll read(){ char c=getchar();ll s=0,f=1; while(c<‘0‘||c>‘9‘){ if(c==‘-‘) f=-1; c=getchar(); } while(c>=‘0‘&&c<=‘9‘){ s=s*10+c-‘0‘; c=getchar(); } return s*f; } inline void write(ll x){ if(x<0) { putchar(‘-‘); x=-x; } if(x>9) write(x/10); putchar(x%10+‘0‘); } int main(){ k=read(); for(int i=1;i<=k;i++) { a[i]=read(); r[i]=read(); } for(int i=2;i<=k;i++) { //两两合并 ll r1=r[i-1],r2=r[i],a1=a[i-1],a2=a[i]; ll g=gcd(a1,a2); if((r2-r1)%g) { printf("-1"); return 0; } a[i]=a1*a2/g; r[i]=((inv(a1/g,a2/g)*((r2-r1)/g)%(a2/g)*a1+r1)%a[i]+a[i])%a[i]; } write(r[k]); return 0; }