#include using namespace std; using ll = long long; int main() { ll X[3], Y[3]; for (ll i = 0; i < 3; i++) { cin >> X[i] >> Y[i]; } ll ans = X[0]; int cnt = 0; while (ans % Y[1] != X[1] && cnt <= Y[1]) ans += Y[0], cnt++; if (cnt > Y[1]) { cout << -1 << endl; return 0; } cnt = 0; while (ans % Y[2] != X[2] && cnt <= Y[2]) ans += lcm(Y[0], Y[1]), cnt++; if (cnt > Y[2]) ans = -1; cout << ans << endl; }