#include #define int long long #define pii pair #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(c) (c).begin(),(c).end() #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) #define MINF(a) memset(a,0x3f,sizeof(a)) #define POW(n) (1LL<<(n)) #define IN(i,a,b) (a <= i && i <= b) using namespace std; template inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; } template inline bool CHMAX(T& a,T b) { if(a inline void SORT(T& a) { sort(ALL(a)); } template inline void REV(T& a) { reverse(ALL(a)); } template inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); } const int MOD = 1000000007; const int INF = 0x3f3f3f3f3f3f3f3f; const double EPS = 1e-10; /* ---------------------------------------------------------------------------------------------------- */ map prime_factor(int x) { map res; for (int i = 2; i*i <= x; i++) { while (x % i == 0) { res[i]++; x /= i; } } if (x != 1) res[x]++; return res; } // 拡張gcd int ext_gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int q = a/b; int g = ext_gcd(b,a-q*b,x,y); int z = x-q*y; x = y; y = z; return g; } // 逆元 | a と m は互いに素 int inv_mod(int a, int m) { int x,y; ext_gcd(a,m,x,y); x %= m; if (x < 0) x += m; return x; } // garner のアルゴリズム | (m_i,m_j) は互いに素 int garner(vector r, vector m) { int n = r.size(); int x = r[0]%m[0]; int mul = m[0]; for (int i = 1; i < n; i++) { int t = (r[i]-x)*inv_mod(mul,m[i])%m[i]; if (t < 0) t += m[i]; (x += t*mul) %= MOD; (mul *= m[i]) %= MOD; } return x; } signed main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); int N; cin >> N; map mp,mx; REP(i,N) { int X,Y; cin >> X >> Y; map pf = prime_factor(Y); for (auto p : pf) { int mul = 1; for (int j = 0; j < p.second; j++) { mul *= p.first; if (mp.count(mul)) { if (mp[mul] != X%mul) { cout << -1 << endl; return 0; } } mp[mul] = X%mul; CHMAX(mx[p.first],mul); } } } vector r,m; for (auto p : mx) { r.push_back(mp[p.second]); m.push_back(p.second); } cout << garner(r,m) << endl; return 0; }