#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define LONG_INF 10000000000000000 #define MAX_MOD 1000000007 #define REP(i,n) for(long long i = 0;i < n;++i) long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a%b); } long long lcm(long long a, long long b) { return a*b / gcd(a, b); } int main() { long long x[3], y[3]; REP(i, 3) { cin >> x[i] >> y[i]; } for (long long i = 0;i <= 100000000;++i) { long long hoge = x[0] + y[0] * i; if (hoge % y[1] == x[1]) { for (long long j = 0;j <= 100000000;++j) { long long gyao = hoge + lcm(y[0], y[1]) * j; if (gyao % y[2] == x[2]) { cout << gyao << endl; return 0; } } cout << -1 << endl; return 0; } } cout << -1 << endl; return 0; }