#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#define ENVIRONMENT_LINKED_ACL #ifdef ENVIRONMENT_LINKED_ACL #include #include #include #endif //#define ENVIRONMENT_LINKED_BOOST #ifdef ENVIRONMENT_LINKED_BOOST #include #include #endif using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; #define REP(i, a, b) for (ll i = a; i < b; ++i) #define REPREV(i, a, b) for (ll i = a; i > b; --i) const int _ = []() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(10); return 0; }(); template void resize(value_t& v, const value_t& val) { v = val; } template void resize(std::vector& v, const value_t& val, int size, arg_t... arg) { v.resize(size); for (auto& c : v) resize(c, val, arg...); } template void chmin(A& a, const B& b) { a = min(a, static_cast(b)); }; template void chmax(A& a, const B& b) { a = max(a, static_cast(b)); }; //最大公約数 template T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } //最小公倍数 template T 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 std::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 std::pair CRT(std::vector>& v) { pair ans = {0, 1}; for (auto p : v) ans = CRT(ans.first, ans.second, p.first, p.second); return ans; } int main() { vector> v(3); REP(i, 0, 3) { cin >> v[i].first >> v[i].second; } auto ret = CRT(v); if (ret.second == 0) cout << -1 << endl; else cout << ret.first << endl; return 0; }