#include #define rep(i, a) for (int i = 0; i < (a); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define repr(i, a) for (int i = (a) - 1; i >= 0; i--) #define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--) using namespace std; typedef long long ll; const ll inf = 1e9; const ll mod = 1e9 + 9; ll x[3], y[3]; ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; } void solve(ll x1, ll y1, ll x2, ll y2, ll &resX, ll &resY) { ll l = lcm(y1, y2); for (ll i = 0; x1 + y1 * i < l; i++) { ll z = x1 + y1 * i; if (z % y2 == x2) { resX = z; resY = l; return; } } resX = 0; resY = -1; } int main() { rep (i, 3) cin >> x[i] >> y[i]; solve(x[0], y[0], x[1], y[1], x[1], y[1]); if (y[1] == -1) { cout << -1 << endl; return 0; } solve(x[1], y[1], x[2], y[2], x[2], y[2]); if (y[2] == -1) { cout << -1 << endl; return 0; } cout << x[2] << endl; }