#include using namespace std; typedef long long ll; const ll INF = 1e18; ll solve(ll x1, ll y1, ll x2, ll y2, ll limit) { for (ll i = x1; i < limit; i += y1) { if (i % y2 == x2) return i; } return -1; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll x[3], y[3]; for (int i = 0; i < 3; i++) cin >> x[i] >> y[i]; ll l1 = y[0] / __gcd(y[0], y[1]) * y[1]; ll ans = solve(x[0], y[0], x[1], y[1], l1); if (ans == -1) { cout << -1 << endl; return 0; } ll l2 = l1 / __gcd(l1, y[2]) * y[2]; ans = solve(ans % l1, l1, x[2], y[2], l2); cout << ans << endl; return 0; }