#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define endl '\n' #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end()) typedef long long ll; typedef long double ld; typedef pair P; typedef complex comp; typedef vector< vector > matrix; struct pairhash { public: template size_t operator()(const pair &x) const { size_t seed = hash()(x.first); return hash()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2); } }; const ll INF = 1e9 + 9; const ll MOD = 1e9 + 7; int n; ll x[1010], y[1010]; class Garner { private: vector as; vector ms; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); } // return: gcd(a, b) // ax + by = gcd(a, b) を満たす (x, y) が格納される ll ext_gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = ext_gcd(b, a%b, y, x); y -= a/b * x; return d; } // ax = 1 (mod m) となる x を返す // gcd(a, m) != 1 の場合 -1 を返す ll mod_inverse(ll a, ll m) { ll x, y; ll g = ext_gcd(a, m, x, y); if (g > 1) return -1; if (x < 0) return m+x; return x; } bool make_coprime() { const int sz = as.size(); for (int i = 0; i < sz; i++) { for (int j = i+1; j < sz; j++) { ll g = gcd(ms[i], ms[j]); if ((as[i] - as[j]) % g != 0) return false; ms[i] /= g; ms[j] /= g; ll gi = gcd(ms[i], g); ll gj = g / gi; do { g = gcd(gi, gj); gi *= g; gj /= g; } while (g != 1); ms[i] *= gi; ms[j] *= gj; as[i] %= ms[i]; as[j] %= ms[j]; } } return true; } public: void add_constraint(const ll a, const ll m) { as.push_back((a%m+m)%m); ms.push_back(m); } ll calc(const bool is_coprime, const ll MOD = 0) { if (!is_coprime) { // m を互いに素にする if (!make_coprime()) return -1; } if (MOD == 0) { const int sz = as.size(); ll m_prod = 1; ll x = as[0] % ms[0]; for (int i = 1; i < sz; i++) { m_prod *= ms[i-1]; ll t = (as[i] - x) * mod_inverse(m_prod, ms[i]) % ms[i]; if (t < 0) t += ms[i]; x += t * m_prod; } return x; } else { ms.push_back(MOD); const int sz = ms.size(); vector coeffs(sz, 1); vector constants(sz, 0); for (int i = 0; i < (int)as.size(); i++) { ll t = (as[i] - constants[i]) * mod_inverse(coeffs[i], ms[i]) % ms[i]; if (t < 0) t += ms[i]; for (int j = i+1; j < sz; j++) { constants[j] += t * coeffs[j]; constants[j] %= ms[j]; coeffs[j] *= ms[i]; coeffs[j] %= ms[j]; } } return constants.back(); } } }; ll solve() { Garner g; for (int i = 0; i < n; i++) { g.add_constraint(x[i], y[i]); } return g.calc(false, MOD); } void input() { cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); input(); cout << solve() << endl; }