//Easyの解法(条件式を1つずつ追加していく)を高速化. -> Hard, Mod取るのが面倒くさいなあ… //((a % mod) % c == (b % mod) % c ⇔ a % c == b % cではないので) #include #define int long long using namespace std; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } int lcm(int a, int b) { return a * b / gcd(a, b); } signed main() { int x[3], y[3]; int i, j; for (i = 0; i < 3; i++) cin >> x[i] >> y[i]; int ans = 0; int t = 1; for (i = 0; i < 3; i++) { int diff = t % y[i]; if (diff == 0) { if (ans % y[i] != x[i]) { cout << -1 << endl; return 0; } } else { int rem = (x[i] - ans + y[i]) % y[i]; if (rem % diff != 0) { cout << -1 << endl; return 0; } ans += t * rem / diff; } t = lcm(t, y[i]); } if (ans == 0) { cout << t << endl; } else { cout << ans << endl; } return 0; }