#include #define rep(i,n) for(int i=0;i<(n);++i) #define rrep(i,n) for(int i=1;i<(n);++i) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin,(a).rend() #define dunk(a) cout << (a) << endl using namespace std; typedef long long ll; const int inf = 1001001001; const int mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { ios::sync_with_stdio(false); cin.tie(0); vector x(3), y(3); rep(i, 3) cin >> x[i] >> y[i]; ll ans; ll pin = y[1] + 1; bool elf = false; rep(i, pin) { ll res = x[0] + y[0] * i; if (res % y[1] == x[1]) { elf = true; ans = res; break; } } if (!elf) { puts("-1"); return 0; } elf = false; pin = y[2] + 1; rep(i, pin) { ll res = ans + lcm(y[0], y[1]) * i; if (res % y[2] == x[2]) { elf = true; ans = res; break; } } if (!elf) { puts("-1"); return 0; } dunk(ans != 0 ? ans : lcm(y[0], lcm(y[1], y[2]))); return 0; }