#include #include #include #include #include #include #include #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; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } long long extgcd(long long a, long long b, long long& p, long long& q){ if(b == 0) { p = 1; q = 0; return a; } long long d = extgcd(b, a % b, q, p); q -= a / b * p; return d; } pair crt(const vector& rs, const vector& ms){ if(rs.empty() || ms.empty()) return make_pair(0, 1); long long R = rs[0], M = ms[0]; for(int i = 1; i < (int)rs.size(); ++i){ long long p, q, r = rs[i], m = ms[i]; if(M < m){ swap(M, m); swap(R, r); } long long d = extgcd(M, m, p, q); if((r - R) % d != 0) return make_pair(0, -1); long long modulo = m / d; long long tmp = (r - R) / d % modulo * p % modulo; R += M * tmp; M *= modulo; } if((R %= M) < 0) R += M; return make_pair(R, M); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); vector X(3), Y(3); for(int i = 0; i < 3; ++i) cin >> X[i] >> Y[i]; auto ans = crt(X, Y); cout << (ans.second == -1 ? ans.second : ans.first) << endl; return 0; }