#include #include using namespace std; namespace mp = boost::multiprecision; using Bint = mp::cpp_int; Bint safe_mod(Bint x, Bint m) { x %= m; if (x < 0) x += m; return x; } std::pair inv_gcd(Bint a, Bint b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; Bint s = b, t = a; Bint m0 = 0, m1 = 1; while (t) { Bint u = s / t; s -= t * u; m0 -= m1 * u; auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return {s, m0}; } std::pair crt(const std::vector& r, const std::vector& m) { assert(r.size() == m.size()); int n = int(r.size()); Bint r0 = 0, m0 = 1; for (int i = 0; i < n; i++) { assert(1 <= m[i]); Bint r1 = r[i], m1 = m[i]; if (m0 < m1) { std::swap(r0, r1); std::swap(m0, m1); } if (m0 % m1 == 0) { if (r0 % m1 != r1) return {0, 0}; continue; } Bint g, im; std::tie(g, im) = inv_gcd(m0, m1); Bint u1 = (m1 / g); if ((r1 - r0) % g) return {0, 0}; Bint x = (r1 - r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if (r0 < 0) r0 += m0; } return {r0, m0}; } int main(){ int n; cin >> n; vector r(n), m(n); for(int i = 0; i < n; i++){ cin >> r[i] >> m[i]; } auto ans = crt(r, m); if(ans.second == 0){ cout << -1 << '\n'; return 0; } if(ans.first == 0)ans.first += ans.second; cout << ans.first % 1000000007 << '\n'; }