#include using namespace std; #define rep(i, n) for (decltype(n) i = 0; i < (n); ++i) #define rep3(i, s, n) for (decltype(n) i = (s); i < (n); ++i) #define repR(i, n) for (decltype(n) i = (n)-1; i >= 0; --i) #define rep3R(i, s, n) for (decltype(n) i = (n)-1; i >= (s); --i) #define repit(it, c) for (auto it = (c).begin(); it != (c).end(); ++it) #define all(x) ::std::begin(x), ::std::end(x) using ll = long long; template inline bool chmax(T ¤t, const U &test) { if (current < test) { current = test; return true; } return false; } template inline bool chmin(T ¤t, const U &test) { if (current > test) { current = test; return true; } return false; } const int64_t INFL = pow(10, 18); const int INF = 2 * pow(10, 9); ll ext_Euclid(ll a, ll b, ll &p, ll &q) { if (b == 0) { p = 1; q = 0; return a; } ll d = ext_Euclid(b, a % b, q, p); q -= a / b * p; return d; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); constexpr char endl = '\n'; /////////////////////////// vector x(3), y(3); rep(i, 3) { cin >> x[i] >> y[i]; } ll ans = 0; ll mod = 1; rep(i, 3) { // mod p + y[i] q == gcd(mod, y[i]) ll p, q; ll d = ext_Euclid(mod, y[i], p, q); if ((ans - x[i]) % d != 0) { cout << -1 << endl; return 0; } ll s = (ans - x[i]) / d; // s mod p + s y[i] q == ans - x[i] ans = ans - mod * (s * p % (y[i] / d)); mod = mod / d * y[i]; ans = (ans % mod + mod) % mod; } if (ans == 0) { ans = y[0]; ll gcd = y[0]; ll p, q; rep(i, 2) { gcd = ext_Euclid(ans, y[1 + i], p, q); ans = ans * y[1 + i] / gcd; } } cout << ans << endl; }