#include #define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++) const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; typedef long long ll; ll X[3], Y[3]; int main() { cin.tie(0); ios::sync_with_stdio(false); for (int i = 0; i < 3; i++) { cin >> X[i] >> Y[i]; } for (ll i = 0; i < Y[1]; i++) { ll tmp = Y[0] * i + X[0]; if ((tmp-X[1]) % Y[1] == 0) { ll lcm = Y[0] / __gcd(Y[0], Y[1]) * Y[1]; for (ll j = 0; j < Y[2]; j++) { ll ans = tmp+j*lcm; if ((ans-X[2]) % Y[2] == 0) { cout << ans << endl; return 0; } } } } cout << -1 << endl; return 0; }