#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b,LL &x,LL &y)
{
if(!b){
x=1,y=0;
return a;
}
LL d=gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main()
{
int n,i;
LL a1,a2,m1,m2,k1,k2,d,t;
bool ans=true;
scanf("%d%lld%lld",&n,&a1,&m1);
for(i=0;i<n-1;i++){
scanf("%lld%lld",&a2,&m2);
d=gcd(a1,a2,k1,k2);
if((m2-m1)%d){
ans=false;
break;
}
k1*=(m2-m1)/d,t=a2/d,k1=(k1%t+t)%t,m1=a1*k1+m1,a1=abs(a1/d*a2);
}
if(ans)printf("%lld\n",(m1%a1+a1)%a1);
else printf("-1");
return 0;
}