#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; #define REP(i,a,b) for(ll i = a; i < b; ++i) #define PRI(s) std::cout << s << endl #define PRIF(v, n) printf("%."#n"f\n", (double)v) templatevoid mins(A& a, const B& b) { a = min(a, (A)b); }; templatevoid maxs(A & a, const B & b) { a = max(a, (A)b); }; template void cumulative_sum(const vector& src, vector& ans) { ans.resize(src.size() + 1); ans[0] = 0; REP(i, 0, src.size()) ans[i + 1] = ans[i] + src[i]; } #include using namespace boost::multiprecision; //素因数分解 std::vector> factorize(long long n) { std::vector> res; for (long long i = 2; i * i <= n; ++i) { if (n % i != 0) continue; res.emplace_back(i, 0); while (n % i == 0) { n /= i; res.back().second++; } } if (n != 1) res.emplace_back(n, 1); return res; } ll check(vector>& v, ll a, ll b, ll ind) { if (ind == v.size()) { if (abs(a - b) == 1) return min(a, b); else return 1e18; } ll tmp = 1; for (int i = 0; i < v[ind].second; ++i) tmp *= v[ind].first; return min(check(v, a * tmp, b, ind + 1), check(v, a, b * tmp, ind + 1)); } //最大公約数 templateT gcd(T a, T b) { if (b == 0)return a; return gcd(b, a % b); } //最小公倍数 templateT lcm(T a, T b) { return a * b / gcd(a, b); } //拡張ユークリッドの互除法。戻り値は最大公約数、x,yはax+by=gcd(a,b)を満たす組の一つ template T gcdext(T a, T b, T& x, T& y) { if (b == 0) { x = 1; y = 0; return a; } T g = gcdext(b, a % b, y, x); y -= a / b * x; return g; } //中国剰余定理 x % mod1 == v1 % mod1, x % mod2 == v2 % mod2 と同値な合同式 x % lcm(mod1, mod2) == r % lcm の(r, lcm)を求める。 //解が存在する条件は、v1 % gcd(mod1, mod2) == v2 % gcd(mod1, mod2) template pair CRT(T v1, T mod1, T v2, T mod2) { if (mod1 <= 0 || mod2 <= 0) return { 0,0 }; T p, q; T g = gcdext(mod1, mod2, p, q); if ((v2 - v1) % g != 0)return { 0, 0 }; T s = (v2 - v1) / g; T mod = lcm(mod1, mod2); T r = v1 + s * mod1 % mod * p % mod; return { (r % mod + mod) % mod, mod }; } template pair CRT(vector>& v) { pair ans = { 0,1 }; for (auto p : v) ans = CRT(ans.first, ans.second, p.first, p.second); return ans; } int main() { pair ans = { 0,1 }; for (int i = 0; i < 3; ++i) { cpp_int x, y; cin >> x >> y; ans = CRT(ans.first, ans.second, x, y); } if (ans.second == 0) PRI(-1); else PRI(ans.first); return 0; }