#include #include #include #include #include using namespace std; typedef long long ll; typedef __int128_t lll; #define abs(a) (a > 0 ? (a) : -(a)) #define printf(...) lll extgcd(lll a, lll b, lll &x, lll &y) { for (lll u = y = 1, v = x = 0; a; ) { lll q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; } lll gcd(lll a, lll b){ return b ? gcd(b,a%b) : a; } lll lcm(lll a, lll b){ return a/gcd(a,b)*a; } lll modinv(lll x, lll m) { lll s, t; extgcd(x, m, s, t); return (m+s) % m; } lll mul(lll a, lll b, lll m){ if(a < 0) a = m - abs(a)%m; else a %= m; if(b < 0) b = m - abs(b)%m; else b %= m; return a*b%m; } // x, mod pair crt(lll a1, lll m1, lll a2, lll m2){ lll k,k2; lll g = extgcd(m1,m2,k,k2); if(abs(a1-a2)%g != 0){ return make_pair(-1,-1); } else { lll l = m1 / g * m2; // k *= (a2-a1)/g; // lll x = (a1 + mul(k,m1,l)) % l; // return make_pair(x,l); lll xx = a1 + mul(mul((a2 - a1) / g, k, l) , m1, l); return make_pair(xx,l); } } int n; lll x[1010], y[1010]; pair solve(){ lll mod = 1, ans = 0; for(int i = 0; i < 3; i++){ tie(ans,mod) = crt(ans, mod, x[i], y[i]); if(ans == -1) return make_pair(-1,-1); } return make_pair(ans,mod); } int main(){ while(1){ for(int i = 0; i < 3; i++){ ll xx,yy; if(!(cin >> xx >> yy)) exit(0); x[i] = xx, y[i] = yy; } lll ans,m; tie(ans,m) = solve(); if(ans == 0) ans = m; cout << (ll)ans << endl; } }