#ifdef LOCAL111 #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #define _USE_MATH_DEFINES #include const int INF = 1e9; using namespace std; template ostream& operator<< (ostream& os, const pair& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; } #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define SZ(a) int((a).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #ifdef LOCAL111 #define DEBUG(x) cout<<#x<<": "<<(x)< void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} #else #define DEBUG(x) true template void dpite(T a, T b){ return; } #endif #define F first #define S second #define SNP string::npos #define WRC(hoge) cout << "Case #" << (hoge)+1 << ": " template void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} template bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} template long long gcd(T x, T y){ return y==0 ? x : gcd(y, x%y); } //res.first*a+res.second*b == 1 となるresを返す (a,bは互いに素) pair extgcd(long long a,long long b) { if(b==1){ return pair(0,1); } pair t=extgcd(b,a%b); return pair(t.second,t.first-a/b*t.second); } //modの逆元を返す long long inverse(long long a,long long modl) { return (extgcd(modl,a).second+modl)%modl; } // x = second (mod first) long long garner(const vector>& ex) { int n = ex.size(); long long res = 0; long long k = 1; for(int i = 0; i < n; ++i) { DEBUG(i); DEBUG(res); long long x, m; tie(m,x) = ex[i]; long long g = gcd(k,m); m /= g; k /= g; if((x-res)%g != 0) return -1; DEBUG(m); DEBUG(k); DEBUG((x-res)/g); long long inv = inverse(k,m); long long v = (x-res)/g*inv; DEBUG(v); res += v*k*g; k = k*m*g; } return res; } long long garner(const vector& x, const vector& mod){ int n = x.size(); vector> v(n); for(int i = 0; i < n; ++i) { v[i] = {mod[i],x[i]}; } return garner(v); } long long use_garner(vector& x, vector& mod){ int n = x.size(); for(int i = 0; i < n; ++i) { x[i] %= mod[i]; } if(n == 1) return x[0]; long long g = gcd(mod[0],mod[1]); for(int i = 2; i < n; ++i) { g = gcd(g,mod[i]); } vector> ex(n); for(int i = 0; i < n; ++i) { mod[i] /= g; if(x[i]%g != 0) return -1; x[i] /= g; ex[i] = {mod[i],x[i]}; } ex.erase(unique(ex.begin(), ex.end()),ex.end()); for(int i = 0; i < n-1; ++i) { if(ex[i].first == ex[i+1].second) return -1; } dpite(ALL(ex)); return garner(ex); } typedef long long int LL; typedef unsigned long long ULL; typedef pair P; void ios_init(){ //cout.setf(ios::fixed); //cout.precision(12); #ifdef LOCAL111 return; #endif ios::sync_with_stdio(false); cin.tie(0); } int main() { ios_init(); int n; while(cin >> n) { vector x(n); vector y(n); REP(i,n) cin >> x[i] >> y[i]; cout << garner(x,y) << endl; } return 0; }