#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using i128 = __int128_t; template using fn = function; #define all(a) a.begin(), a.end() #define allr(a) a.rbegin(), a.rend() template int len(const A &a) { return a.size(); } #define eb emplace_back #define pb push_back #define elif else if template using vec = vector; template using vec2 = vec>; template using vec3 = vec>; template using vec4 = vec>; template using vec5 = vec>; #define VEC(T, a, ...) \ vec a(__VA_ARGS__) #define VEC2(T, a, n, ...) \ vector a(n, vec(__VA_ARGS__)); #define VEC3(T, a, n, m, ...) \ vector a( \ n, \ vector(m, vec(__VA_ARGS__)) \ ); #define VEC4(T, a, n, m, l, ...) \ vector a( \ n, \ vector( \ m, \ vector(l, vec(__VA_ARGS__)) \ ) \ ); #define eval_4(a, b, c, d, e, ...) e #define loop while (1) #define rep(n) \ for (int __ = 0; __ < n; __++) #define range_1(i, n) \ for (int i = 0; i < n; i++) #define range_2(i, a, b) \ for (ll i = a; i < b; i++) #define range_3(i, a, b, c) \ for (ll i = a; i < b; i += c) #define range(...) \ eval_4(__VA_ARGS__, range_3, range_2, range_1, rep)( \ __VA_ARGS__ \ ) #define ranger_1(i, n) \ for (int i = n; i-- > 0;) #define ranger_2(i, a, b) \ for (ll i = b; i-- > a;) #define ranger_3(i, a, b, c) \ for (ll i = b - 1; i >= a; i -= c) #define range_rev(...) \ eval_4(__VA_ARGS__, ranger_3, ranger_2, ranger_1)( \ __VA_ARGS__ \ ) #define iter(x, a) \ for (const auto &x : a) #define iter_mut(x, a) \ for (auto &&x : a) template istream & operator>>(istream &in, pair &p) { return in >> p.first >> p.second; } template ostream &operator<<( ostream &out, pair &p ) { out << p.first << ' ' << p.second; return out; } template void read_tup(istream &in, T &x) { if constexpr (tuple_size::value > k) { in >> get(x); read_tup(in, x); } } template istream &operator>>( istream &in, tuple &x ) { read_tup(in, x); return in; } template void out_tup(ostream &out, T &x) { if constexpr (tuple_size::value > k) { if constexpr (k > 0) { out << ' '; } out << get(x); out_tup(out, x); } } template ostream &operator<<( ostream &out, tuple &x ) { out_tup(out, x); return out; } template auto operator<<(ostream &out, vec a) -> ostream & { range(i, len(a)) { if (i) { out << ' '; } out << a[i]; } return out; } template auto operator<<(ostream &out, vec2 a) -> ostream & { iter_mut(x, a) out << x << '\n'; return out; } template auto operator>>(istream &in, vec &a) -> istream & { iter_mut(x, a) in >> x; return in; } template void in(T &...a) { (cin >> ... >> a); } template void out(T a, const U... b) { cout << a; ((cout << ' ' << b), ...); cout << '\n'; } template vec iota(int n, T v = 0) { vec a(n); std::iota(all(a), v); return a; } template using max_queue = priority_queue; template using min_queue = priority_queue, greater>; template T pop(queue &q) { T v = q.front(); q.pop(); return v; } template T pop(deque &q) { T v = q.front(); q.pop_front(); return v; } template T pop(vec &q) { T v = q.back(); q.pop_back(); return v; } template T pop(max_queue &q) { T v = q.top(); q.pop(); return v; } template T pop(min_queue &q) { T v = q.top(); q.pop(); return v; } template T max(const vec &a) { return *max_element(all(a)); } template T min(const vec &a) { return *min_element(all(a)); } int topbit(int x) { return 31 - __builtin_clz(x); } int popcnt(int x) { return __builtin_popcount(x); } template int ctz(T n) { return __builtin_ctzll(n); } template int bitlen(T n) { return 64 - __builtin_clzll(n); } template bool operator==( const vec &a, const vec &b ) { int n = len(a); if (len(b) != n) { return false; } range(i, n) { if (a[i] != b[i]) { return false; } } return true; } template bool chmin(T &a, const U &b) { return b < a ? a = b, 1 : 0; } template bool chmax(T &a, const U &b) { return b > a ? a = b, 1 : 0; } template T sum(const vec &a) { return accumulate(all(a), 0ll); } template void unique(vec &a) { sort(all(a)); a.erase(std::unique(all(a)), a.end()); } template int lb(const A &a, const T &x) { auto p = lower_bound(all(a), x); return distance(a.begin(), p); } template int ub(const A &a, const T &x) { auto p = upper_bound(all(a), x); return distance(a.begin(), p); } template vec argsort(const A &a) { int n = len(a); auto b = iota(n); sort(all(b), [&](int i, int j) { return a[i] < a[j]; }); return b; } template auto divmod(T a, T b) -> pair { T q = a / b; return {q, a - q * b}; } template int argmax(const vec &a) { int n = len(a); assert(n); int j = 0; range(i, 1, n) { if (a[i] > a[j]) { j = i; } } return j; } #ifdef DEBUG #define dbg(...) out(__VA_ARGS__); #else #define dbg(...) ; #endif #ifdef DEBUG #define dbg_assert(...) \ assert(__VA_ARGS__); #else #define dbg_assert(...) ; #endif // define yes/no #define yesno(y, n) \ void yes(bool f = 1) { \ out(f ? #y : #n); \ } \ void no() { \ out(#n); \ } yesno(yes, no); // yesno(Yes, No); // yesno(YES, NO); template T norm(T m, T x) { assert(m > 0); if (x < -m || x >= m) { x %= m; } if (x < 0) { x += m; } return x; } template tuple egcd(T a, T b) { if (!b) { if (a < 0) { return {-a, -1, 0}; } return {a, 1, 0}; }; T q = a / b; auto [g, x, y] = egcd(b, a - b * q); return {g, y, x - q * y}; } pair crt_mod( vec m, const vec &r, int mod ) { int l = len(m); assert(len(r) == l); m.pb(mod); vec p(l + 1, 1), x(l + 1); range(i, l) { ll b = r[i], n = m[i]; assert(n > 0); auto [g, ip, _] = egcd(p[i], n); auto [q, r] = divmod(b - x[i], g); if (r) { return {-1, -1}; } n /= g; ll t = norm(n, q % n * ip); range(j, i + 1, l + 1) { (x[j] += t * p[j]) %= m[j]; (p[j] *= n) %= m[j]; } } return {x.back(), p.back()}; } int mpow(int m, int x, ll t) { assert(t >= 0 && m > 0); if (!t) { return 1; } ll y = mpow(m, x, t >> 1); y = y * y % m; if (t & 1) { y = y * x % m; } return y; } template auto factorize(T n) -> vec> { vec> f; for (T i = 2; i * i <= n; ++i) { if (n % i) { continue; } int e = 0; do { ++e; n /= i; } while (n % i == 0); f.eb(i, e); } if (n != 1) { f.eb(n, 1); } return f; } template map factorize_lcm(const vec &a) { map c; iter(x, a) { for (auto &[p, e] : factorize(x)) { chmax(c[p], e); } } return c; } template int lcm_mod(int m, const vec &a) { auto f = factorize_lcm(a); ll l = 1; for (auto &[p, e] : f) { l = l * mpow(m, p, e) % m; } return l; } void solve() { int n; in(n); vec r(n), m(n); range(i, n) { in(r[i], m[i]); } const int mod = 1'000'000'007; auto [x, l] = crt_mod(m, r, mod); if (l == -1) { out(-1); return; } if (max(r) == 0) { out(l); } else { out(x); } } int main() { ios::sync_with_stdio(0); cin.tie(0); // cout << setprecision(16); int t = 1; // in(t); while (t--) { solve(); } }