#include using namespace std; using ll = long long; /// g:gcd(a, b), ax+by=g struct EG { ll g, x, y; }; EG ext_gcd(ll a, ll b) { if (b == 0) { if (a >= 0) return EG{a, 1, 0}; else return EG{-a, -1, 0}; } else { auto e = ext_gcd(b, a % b); return EG{e.g, e.y, e.x - a / b * e.y}; } } ll inv_mod (ll x, ll md) { auto z = ext_gcd(x, md).x; return (z % md + md) % md; } template T pow_mod(T x, U n, T md) { T r = 1 % md; x %= md; while (n) { if (n & 1) r = (r * x) % md; x = (x * x) % md; n >>= 1; } return r; } // (rem, mod) pair crt(const vector& b, const vector& c) { int n = int(b.size()); ll r = 0, m = 1; for (int i = 0; i < n; i++) { auto eg = ext_gcd(m, c[i]); ll g = eg.g, im = eg.x; if ((b[i] - r) % g) return {0, -1}; ll tmp = (b[i] - r) / g * im % (c[i] / g); r += m * tmp; m *= c[i] / g; } return {(r % m + m) % m, m}; } ll garner(const vector& b, vector m, ll mod) { m.push_back(mod); int n = m.size(); vector coeffs(n, 1); vector consts(n, 0); for(int k = 0; k < n - 1; ++k) { ll t = (b[k] - consts[k]) * inv_mod(coeffs[k], m[k]) % m[k]; if(t < 0) t += m[k]; for(int i = k + 1; i < n; ++i) { consts[i] = (consts[i] + t * coeffs[i]) % m[i]; coeffs[i] = coeffs[i] * m[k] % m[i]; } } return consts.back(); } void coprimize_simulaneous_congruence_equation(ll& r1, ll& m1, ll& r2, ll& m2) { ll g = gcd(m1, m2); if((r2 - r1) % g != 0) { r1 = r2 = m1 = m2 = -1; return; } m1 /= g, m2 /= g; ll gi = gcd(g, m1); ll gj = g / gi; do { g = gcd(gi, gj); gi *= g, gj /= g; } while(g != 1); m1 *= gi, m2 *= gj; r1 %= m1, r2 %= m2; } int main() { vector x(3), y(3); for(int i = 0; i < 3; i++) { cin >> x[i] >> y[i]; } auto [ans, l] = crt(x, y); if(l == -1) cout << "-1\n"; else if(ans == 0) cout << l << endl; else cout << ans << endl; }