#include "bits/stdc++.h" using namespace std; long long gcd(long long a, long long b){ if (b == 0) return a; return gcd(b, a%b); } int main() { long long X1, Y1, X2, Y2, X3, Y3; cin >> Y1 >> X1 >> Y2 >> X2 >> Y3 >> X3; long long ans = Y3; int count = X2 + 10000000; long long add = X3; while (count > 0){ if (ans % X2 == Y2) break; ans += add; count--; } if (count <= 0){ cout << -1 << endl; return 0; } add = (X3 * X2) / gcd(X3, X2); count = X1 + 10000000; while (count > 0){ if (ans % X1 == Y1) break; ans += add; count--; } if (count <= 0){ cout << -1 << endl; return 0; } cout << ans << endl; }