#include using namespace std; using pii = pair; using ll = long long; #define rep(i, j) for(int i=0; i < (int)(j); i++) #define repeat(i, j, k) for(int i = (j); i < (int)(k); i++) #define all(v) v.begin(),v.end() #define debug(x) cerr << #x << " : " << x << endl template bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; } template bool set_max(T &a, const T &b) { return a < b ? a = b, true : false; } // vector template istream& operator >> (istream &is , vector &v) { for(T &a : v) is >> a; return is; } template ostream& operator << (ostream &os , const vector &v) { for(const T &t : v) os << "\t" << t; return os << endl; } // pair template ostream& operator << (ostream &os , const pair &v) { return os << "<" << v.first << ", " << v.second << ">"; } const int INF = 1 << 30; const ll INFL = 1LL << 60; template T gcd(T a,T b){ T c=a,d=b,r; do{r=c%d;c=d;d=r;}while(r); return c; } template T lcm(T a,T b){return a*b/gcd(a,b);} class Solver { public: bool solve() { vector X(3), Y(3); rep(i, 3) cin >> X[i] >> Y[i]; ll A = -1; for(ll k = 0; k <= Y[1]; k++) { ll a = X[0] + k * Y[0]; if(a >= X[1] and (a - X[1]) % Y[1] == 0) { A = a; break; } } ll g = lcm(Y[0], Y[1]); ll AA = -1; for(ll k = 0; k <= Y[2]; k++) { ll a = A + k * g; if(a >= X[2] and (a - X[2]) % Y[2] == 0) { AA = a; break; } } cout << AA << endl; return 0; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Solver s; s.solve(); return 0; }