#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; } template T modinv(T v, T mod) { T x, y; gcdext(v, mod, x, y); return (x % mod + mod) % mod; } //x == v[i].first (mod v[i].second) を満たす最小のxを求める。mは互いに素 template T garner(const std::vector>& v) { int n = v.size(); T m_prod = 1; T x = v[0].first % v[0].second; for (int i = 1; i < n; ++i) { m_prod *= v[i - 1].second; T t = ((v[i].first - x) * modinv(m_prod, v[i].second)) % v[i].second; if (t < 0) t += v[i].second; x += t * m_prod; } return x; } //x == r[i] (mod m[i]) を満たす最小のx mod (mod)を求める。mは互いに素 template T garner(const std::vector>& v, T mod) { int n = v.size(); T m_prod = 1; T x = ((v[0].first % v[0].second) % mod + mod) % mod; for (int i = 1; i < n; ++i) { T tmp = 1; for (int j = 0; j < i; ++j) { tmp *= v[j].second; tmp %= v[i].second; } tmp = modinv(tmp, v[i].second); m_prod *= v[i - 1].second; m_prod %= mod; x += (v[i].first - x) * tmp % mod * m_prod % mod; x = ((x % mod) + mod) % mod; } return x; } int main() { vector> v(3); REP(i, 0, 3) { cin >> v[i].first >> v[i].second; } REP(i, 0, 3) { REP(j, i + 1, 3) { ll g = gcd(v[i].second, v[j].second); if ((v[i].first - v[j].first) % g != 0) { cout << -1 << endl; return 0; } ll afilter = gcd(g, v[i].second / g); ll bfilter = gcd(g, v[j].second / g); ll gcur = g; ll a = 1, b = 1; while (true) { ll tmp = gcd(afilter, gcur); if (tmp == 1) break; a *= tmp; gcur /= tmp; } while (true) { ll tmp = gcd(bfilter, gcur); if (tmp == 1) break; b *= tmp; gcur /= tmp; } ll na = v[i].second / lcm(a, g) * a * gcur; ll nb = v[j].second / lcm(b, g) * b; v[i].second = na; v[j].second = nb; v[i].first %= na; v[j].first %= nb; } } cout << garner(v) << endl; return 0; }