/*
qq:1239198605
ctgu_yyf
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
ll mod[100005],yushu[100005];
ll ex_gcd(ll a,ll b,ll &x,ll &y)
{
ll d=a;
if(b!=0)
{
d=ex_gcd(b,a%b,y,x);
y=y-(a/b)*x;
}
else
{
x=1,y=0;
}
return d;
}
int main()
{
ios::sync_with_stdio(false);
int t;
while(cin>>t)
{
for(int i=1;i<=t;i++)
cin>>mod[i]>>yushu[i];
// mod[1]*x+yushu[1]=k=mod[2]*y+yushu[2] 变形得
// mod[1]*x+mod[2]*(-y)=yushu[2]-yushu[1];
int flag=0;ll x,y;
for(int i=2;i<=t;i++)
{
ll c=yushu[i]-yushu[1];
ll a=mod[1];
ll b=mod[i];
ll d=ex_gcd(a,b,x,y);
ll md;
if(c%d!=0)
{
flag=1;
break;
}
//找最小x
x=x*(c/d); md=mod[i]/d; x= (x%md +md)%md;
//更新第一个方程
yushu[1]=yushu[1]+x*mod[1]; mod[1]=mod[1]*md; yushu[1]=yushu[1]%mod[1];
}
if(flag==1)
cout<<"-1"<<endl;
else
cout<<((yushu[1]%mod[1] + mod[1])%mod[1])<<endl;
}
return 0;
}
关于数论,不是很会。
直接套板子吧。