#include<bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define int __int128
#else
#define int long long
#endif
using namespace std;
int n;
class Faster_IO{
private:
char buf[1<<20],*p1,*p2;
char pbuf[1<<20],*pp;
inline char gc()
{return ((p1==p2)?(p2=(p1=buf)+fread(buf,1,1<<20,stdin)):0),((p1==p2)?(EOF):(*p1++));}
inline bool blank(char ch)
{return (ch==EOF||ch==' '||ch=='\0'||ch=='\n'||ch=='\r'||ch=='\t');}
inline void push(const char &c)
{((pp-pbuf==(1<<20))?(fwrite(pbuf,1,1<<20,stdout),pp=pbuf):0),*pp++=c;}
public:
Faster_IO():p1(buf),p2(buf),pp(pbuf){}
~Faster_IO(){fwrite(pbuf,1,pp-pbuf,stdout);}
template<class T> inline Faster_IO& read(T &x){
bool s=false;x=0;char ch=gc();
for(;!('0'<=ch&&ch<='9');ch=gc())(ch=='-')?(s=true):0;
for(;('0'<=ch&&ch<='9');ch=gc())x=(x<<1)+(x<<3)+(ch^48);
(s)?(x=-x):0;
return *this;
}
inline Faster_IO& read(char *s){
char ch=gc();
for(;blank(ch);ch=gc());
for(;!blank(ch);ch=gc())*s++=ch;
*s='\0';
return *this;
}
inline Faster_IO& read(char &c)
{for(c=gc();blank(c);c=gc());return *this;}
template<class T> inline Faster_IO& write(T x){
(x<0)?(x=-x,push('-')):(void)0;
static T sta[35];
size_t tp=0;
do sta[tp++]=x%10,x/=10;while(x);
while(tp)push(sta[--tp]|48);
return *this;
}
inline Faster_IO& write(char *s,int length)
{for(;length--;)push(*s++);return *this;}
inline Faster_IO& write(char *s)
{return write(s,strlen(s));}
inline Faster_IO& write(const char *s)
{for(;s;)push(*s++);return *this;}
template<class T> inline Faster_IO& write(T x,char lastChar)
{write(x),push(lastChar);return *this;}
inline Faster_IO& write(char *s,int length,char lastChar)
{write(s,length),push(lastChar);return *this;}
}io;
struct LineConEqua{
int a,m;
LineConEqua(){}
inline void input(){io.read(this->m).read(this->a);}
}e[100005];
void exgcd(int a,int b,int &x,int &y){
if(!b)return (void)(x=1,y=0);
exgcd(b,a%b,y,x);
y-=x*a/b;
}
int gcd(int x,int y)
{return y?gcd(y,x%y):x;}
int lcm(int x,int y)
{return x/gcd(x,y)*y;}
int excrt(){
LineConEqua now=e[1];
for(int i=2,x=0,y=0;i<=n;i++){
exgcd(now.m,e[i].m,x,y);
int g=gcd(now.m,e[i].m),k=e[i].a-now.a,l=lcm(now.m,e[i].m);
if(k%g)
return -1;
x=(x*k/g%(e[i].m/g)+(e[i].m/g))%(e[i].m/g);
now.a=((now.m*x+now.a)%l+l)%l,
now.m=l;
}
return (now.a%now.m+now.m)%now.m;
}
signed main(){
io.read(n);
for(int i=1;i<=n;i++)
e[i].input();
io.write(excrt());
return 0;
}