#include #include #include #include #include using namespace std; typedef long long ll; typedef __int128_t lll; #define abs(a) (a > 0 ? a : -a) 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; } // x, mod pair crt(lll a1, lll m1, lll a2, lll m2){ lll k1,k2; lll g = extgcd(m1,m2,k1,k2); if(abs(a1-a2)%g != 0){ return make_pair(-1,-1); } else { lll l = m1 / g * m2; k1 *= (a2-a1)/g; k2 *= (a2-a1)/(-g); assert((a2-a1)%g == 0); assert((a2-a1)%(-g) == 0); assert(m1*k1 - m2*k2 == a2-a1); // printf("%lld %lld %lld %lld\n", (ll)m1,(ll)k1,(ll)m2,(ll)k2); lll x1 = a1 + k1%l*m1%l; lll x2 = a2 + k2%l*m2%l; // printf("x1=%lld x2=%lld\n", (ll)x1,(ll)x2); assert(x1%l == x2%l); return make_pair(x1,l); } } int n; lll x[1010], y[1010]; const lll mod = 1000000007; pair solve(){ lll m = 1, ans = 0; for(int i = 0; i < 3; i++){ tie(ans,m) = crt(ans, m, x[i], y[i]); if(ans == -1) return make_pair(-1,-1); } return make_pair(ans,m); } 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; } }