#include<bits/stdc++.h> #define ll long long using namespace std; ll n,m,a,lcm,now; bool flag; void exgcd(ll a,ll b,ll &d,ll &x,ll &y) { if(b==0) { d=a; x=1; y=0; } else { exgcd(b,a%b,d,x,y); ll t=x; x=y; y=t-a/b*y; } } int main() { ll d,x,y,k; while(~scanf("%lld",&n)) { scanf("%lld%lld",&m,&a); lcm=m;now=a;flag=true; for(int i=1;i<n;i++) { scanf("%lld%lld",&m,&a); a=((a%m)-(now%m)+m)%m; exgcd(lcm,m,d,x,y); if(a%d==0) k=(x*(a/d)%(m/d)+(m/d))%(m/d); else flag=false; now+=k*lcm; lcm=lcm*m/d; now=((now%lcm)+lcm)%lcm; } if(!flag) printf("-1"); else printf("%lld",now); puts(""); } return 0; }