#include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; //-------------------------------------------------------------------- #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define overload4(_1,_2,_3,_4,name,...) name #define rep1(n) for(ll _=0;_<(ll)n;++_) #define rep2(i,n) for(ll i=0;i<(ll)n;++i) #define rep3(i,a,b) for(ll i=(ll)a;i<(ll)b;++i) #define rep4(i,a,b,c) for(ll i=(ll)a;i<(ll)b;i+=(ll)c) #define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__) #ifdef _DEBUG #define pass(...) __VA_ARGS__ ; #define debug1(a) cerr<<#a<<": "<ostream& operator<<(ostream& os,const pair& pp) {return os << "{" << pp.first << "," << pp.second << "}";} templateostream& operator<<(ostream& os,const vector& VV) {if(VV.empty())return os<<"[]";os<<"[";rep(i,VV.size())os<ostream& operator<<(ostream& os,const set& SS) {if(SS.empty())return os<<"[]";os<<"[";auto ii=SS.begin();for(;ii!=SS.end();ii++)os<<*ii<<(ii==prev(SS.end())?"]":",");return os;} templateostream& operator<<(ostream& os,const map& MM) {if(MM.empty())return os<<"[]";os<<"[";auto ii=MM.begin();for(;ii!=MM.end();ii++)os<<"{"<first<<":"<second<<"}"<<(ii==prev(MM.end())?"]":",");return os;} const int inf = 1 << 30; const ll INF = 1LL << 61; const ld pi = 3.14159265358; const ll mod1 = 1000000007LL; const ll mod2 = 998244353LL; typedef pair P; template inline bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; } template inline bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; } ll modpow(ll n,ll m,ll MOD){ if(m == 0)return 1; if(m < 0)return 0; ll res = 1; n %= MOD; while(m){ if(m & 1)res = (res * n) % MOD; m >>= 1; n *= n; n %= MOD; } return res; } ll mypow(ll n,ll m){ if(m == 0)return 1; if(m < 0)return -1; ll res = 1; while(m){ if(m & 1)res = (res * n); m >>= 1; n *= n; } return res; } inline bool isp(ll n){ bool res = true; if(n == 1 || n == 0)return false; else{ for(ll i = 2;i * i <= n;i++){ if(n % i == 0){ res = false; break; } } return res; } } inline bool Yes(bool b = 1){cout << (b ? "Yes\n":"No\n");return b;} inline bool YES(bool b = 1){cout << (b ? "YES\n":"NO\n");return b;} map primefactor(ll n){ map ma; if(n <= 1)return ma; ll m = n; for(ll i = 2;i * i <= n;i++){ while(m % i == 0){ ma[i]++; m /= i; } } if(m != 1)ma[m]++; return ma; } vector divisor(ll n,bool sorted = true,bool samein = false){ vector res; for(ll i = 1;i * i <= n;i++){ if(n % i == 0){ res.push_back(i); if(i * i != n || samein)res.push_back(n / i); } } if(sorted)sort(all(res)); return res; } ll __lcm(ll a,ll b){return a / __gcd(a,b) * b;} templateT sum(const vector &V){T r=0;for(auto x:V)r+=x;return r;} templateT sum(const initializer_list &V){T r=0;for(auto x:V)r+=x;return r;} //-------------------------------------------------------------------- //b mod m ll pregarner(vector &b,vector& m,ll mod){ rep(i,b.size()){ rep(j,i){ ll g = gcd(m[i],m[j]); if((b[i] - b[j]) % g != 0)return -1; else if(g == 1)continue; m[i] /= g;m[j] /= g; ll gi = gcd(m[i],g),gj = g / gi; do{ g = gcd(gi,gj); gi *= g;gj /= g; }while(g != 1); m[i] *= gi;m[j] *= gj; b[i] %= m[i];b[j] %= m[j]; } } ll res = 1; rep(i,b.size())res = res * m[i] % mod; return res; } long long modinv(long long a,long long p){ long long b = p, u = 1, v = 0; while(b){ long long t = a / b; a -= t * b;std::swap(a, b); u -= t * v;std::swap(u, v); } u %= p; if(u < 0)u += p; return u; } ll garner(vector b,vector m,ll mod){ m.push_back(mod); int s = m.size(); vector coeffs(s,1LL),constants(s,0LL); rep(i,s - 1){ ll cur = (b[i] - constants[i]) * modinv(coeffs[i],m[i]) % m[i]; if(cur < 0)cur += m[i]; rep(j,i + 1,s){ constants[j] = (constants[j] + cur * coeffs[j]) % m[j]; coeffs[j] = coeffs[j] * m[i] % m[j]; } } return constants.back(); } int main(){ myset(); int N; cin >> N; vector m(N),b(N); rep(i,N)cin >> b[i] >> m[i]; bool zero = 1; rep(i,N)if(b[i])zero = 0; ll lcm = pregarner(b,m,mod1); debug(b,m); if(zero)cout << lcm << "\n"; else if(lcm == -1)cout << "-1\n"; else cout << garner(b,m,mod1) << "\n"; }