#include using namespace std; typedef long long ll; const long long MOD1 = 1000000007; const long long MOD2 = 998244353; const long long INF = 1e17; using P = pair; ll mod(ll a, ll m) { return (a % m + m) % m; } ll extgcd(ll A, ll B, ll& X, ll& Y) { ll D = A; if (B != 0) { D = extgcd(B, A % B, Y, X); Y -= (A / B) * X; } else { X = 1; Y = 0; } return D; } pair ChineseRem(vector&b, vector&m) { ll r = 0, M = 1; for (ll i = 0; i < b.size(); i++) { ll p, q; ll d = extgcd(M, m[i], p, q); if ((b[i] - r) % d != 0) return make_pair(0, -1); ll tmp = (b[i] - r) / d * p % (m[i] / d); r += M * tmp; M *= m[i] / d; } return make_pair(mod(r, M), M); } void solve() { vectorX(3), Y(3); for (ll i = 0; i < 3; i++) { cin >> X[i] >> Y[i]; } pair ans = ChineseRem(X, Y); if (ans == make_pair(0LL, -1LL)) {cout << -1 << endl;return;} while (ans.first <= 0) ans.first += ans.second; cout << ans.first << endl; } int main() { ll T = 1; //cin >> T; while (T--) { solve(); } }