#include #include #include #include #include using namespace std; typedef pair PLL; template T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } int main() { long long x1, y1, x2, y2, x3, y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; PLL big, small; long long lcm12 = lcm(y1, y2); if (y1 > y2) { big = make_pair(x1, y1); small = make_pair(x2, y2); } else { big = make_pair(x2, y2); small = make_pair(x1, y1); } long long y12 = lcm12; long long x12 = -1; for (long long i = big.first; i < lcm12; i += big.second) { if (i % small.second == small.first) { x12 = i; break; } } if (x12 == -1) { cout << -1 << endl; return 0; } long long lcm123 = lcm(y12, y3); if (y12 > y3) { big = make_pair(x12, y12); small = make_pair(x3, y3); } else { big = make_pair(x3, y3); small = make_pair(x12, y12); } long long x123 = -1; for (long long i = big.first; i < lcm123; i += big.second) { if (i % small.second == small.first) { x123 = i; break; } } cout << x123 << endl; return 0; }