#include #include #include #include #include using namespace std; using ll = int64_t; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } // a > b template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } // a < b template T safe_mod(const T x, const T m) { T ret = x % m; if (ret < 0) ret += m; return ret; } template T extGCD(const T a, const T b, T &p, T &q) { if (b == 0) { p = 1; q = 0; return a; } T d = extGCD(b, a % b, q, p); q -= a / b * p; return d; } template std::pair crt(T b1, T m1, T b2, T m2) { T p, q; T d = extGCD(m1, m2, p, q); if ((b2 - b1) % d != 0) return std::make_pair(0, -1); T m = m1 * (m2 / d); T tmp = (b2 - b1) / d * p % (m2 / d); T r = safe_mod(b1 + tmp * m1, m); return std::pair(r, m); } // 拡張ユークリッド互除法による中国余剰定理の計算 template std::pair crt(const std::vector &r, const std::vector &m) { assert(r.size() == m.size()); T r_ret = 0, m_ret = 1; for (int64_t i = 0; i < r.size(); i++) { std::pair pair = crt(r_ret, m_ret, r[i], m[i]); r_ret = pair.first; m_ret = pair.second; if (m_ret == -1) return std::make_pair(0, -1); } return std::pair(r_ret, m_ret); } int main() { vector X(3), Y(3); for (int i = 0; i < 3; i++) { cin >> X[i] >> Y[i]; } pair ret = crt(X, Y); cout << ((ret.second == -1) ? -1 : ret.first) << endl; }