#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r,v) auto r = (v) #else #define aut(r,v) __typeof(v) r = (v) #endif #define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x,y) make_pair((x),(y)) #define mset(m,v) memset(m,v,sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL using namespace std; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template inline void amin(T &x, U y) { if(y < x) x = y; } template inline void amax(T &x, U y) { if(x < y) x = y; } inline void fasterLLDivMod(unsigned long long x, unsigned y, unsigned &out_d, unsigned &out_m) { unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m; #ifdef __GNUC__ asm ( "divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (y) ); #else __asm { mov edx, dword ptr [xh]; mov eax, dword ptr [xl]; div dword ptr [y]; mov dword ptr [d], eax; mov dword ptr [m], edx; }; #endif out_d = d; out_m = m; } typedef unsigned int u32; typedef unsigned long long u64; u32 rem_1(const u32 *x, int n, u32 y) { u32 rem = 0, dummy; for(int i = n - 1; i >= 0; -- i) { u64 a = (u64)rem << 32 | x[i]; fasterLLDivMod(a, y, dummy, rem); } return rem; } u32 add(const u32 *x, const u32 *y, int n, u32 *z) { u32 carry = 0; for(int i = 0; i < n; ++ i) { u32 a = x[i], b = y[i]; u64 c = (u64)a + b + carry; z[i] = (u32)c; carry = (u32)(c >> 32); } return carry; } u32 add_1(const u32 *x, int n, u32 y, u32 *z) { u32 carry = y; for(int i = 0; i < n; ++ i) { u64 c = (u64)x[i] + carry; z[i] = (u32)c; carry = (u32)(c >> 32); } return y; } u32 add(const u32 *x, int xn, const u32 *y, int yn, u32 *z) { assert(xn >= yn); u32 carry = add(x, y, yn, z); return add_1(x + yn, xn - yn, carry, z + yn); } u32 mul_1(const u32 *x, int n, u32 y, u32 *z) { u32 carry = 0; for(int i = 0; i < n; ++ i) { u32 a = x[i]; u64 c = (u64)a * y + carry; z[i] = (u32)c; carry = (u32)(c >> 32); } return carry; } struct Int { vector v; Int() { } Int(u32 x) { if(x != 0) v.assign(1, x); } int size() const { return v.size(); } u32 *data() { return v.empty() ? NULL : &v[0]; } const u32 *data() const { return v.empty() ? NULL : &v[0]; } void resize(int n) { v.resize(n); } Int &normalize() { while(!v.empty() && v.back() == 0) v.pop_back(); return *this; } u32 operator%(u32 y) const { return rem_1(data(), size(), y); } Int &mulAssign(const Int &x, u32 y) { int sz = x.size(); resize(sz + 1); v[sz] = mul_1(x.data(), sz, y, data()); return normalize(); } Int &operator+=(const Int &that) { if(size() >= that.size()) { int sz = size(); resize(sz + 1); v[sz] = add(data(), sz, that.data(), that.size(), data()); }else { int sz = that.size(); resize(sz + 1); v[sz] = add(data(), that.data(), sz, data()); } return normalize(); } void swap(Int &that) { v.swap(that.v); } u64 get64() const { if(v.empty()) return 0; if(v.size() == 1) return v[0]; return v[0] | ((u64)v[1] << 32); } }; int exgcd(int a, int b, int &g) { int u = 1, v = 0; while(b) { int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } g = a; return u; } bool crt(Int &a1, Int &n1, int a2, int n2, Int &tmp) { assert(0 <= a2 && a2 < n2); int b = n1 % n2, t, g; t = exgcd(b, n2, g); int n2_g = n2 / g; int d1 = a1 % n2; if((a2 - d1) % g != 0) return false; int d = (a2 - d1) / g; int h = (long long)d * t % n2_g; if(h < 0) h += n2_g; tmp.mulAssign(n1, h); tmp += a1; a1.swap(tmp); n1.mulAssign(n1, n2_g); return true; } int main() { int N = 3; // scanf("%d", &N); Int a, n = 1U, tmp; bool ok = true, zero = true; rep(i, N) { int X, Y; scanf("%d%d", &X, &Y); zero &= Y == 0; ok = ok && crt(a, n, X, Y, tmp); } if(!ok) { puts("-1"); }else { if(zero) a += n; // int ans = a % 1000000007; long long ans = a.get64(); cout << ans << endl; } return 0; }