#include using namespace std; using ll = long long; using P = pair; const ll MOD = 1e9+7; // const ll MOD = 998244353; const ll INF = 1ll<<60; #define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define DEBUG(x) ; #define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl; int dx[4]{0, 1, 0, -1}; int dy[4]{1, 0, -1, 0}; //////////////////// // Chinese remainder theorem inline ll mod(ll a, ll m) { a %= m; if (a < 0) a += m; return a; } ll extGcd(ll a, ll b, ll &p, ll &q) { if (b == 0) { p = 1; q = 0; return a;} ll d = extGcd(b, a%b, q, p); q -= a/b * p; return d; } pair ChineseRem(const vector &b, const vector &m) { assert(b.size() == m.size()); ll r = 0, M = 1; for (ll i = 0; i < (int)b.size(); ++i) { ll p, q; ll d = extGcd(M, m[i], p, q); if ((b[i] - r) % d != 0) return make_pair(0, -1); ll tmp = (b[i] - r) / d * p % (m[i]/d); r += M * tmp; M *= m[i]/d; } return {mod(r, M), M}; } // Chinese remainder theorem // x = b_0(mod, m_0) // x = b_1(mod, m_1) // ... // x = b_N-1(mod, m_N-1) // <=> // x = r(mod, m) // // call : ChineseRem(vectorb, vector) // return : pair(x, m) // return(no solution) : pair(0, -1) int main(int argc, char **argv) { ios_base::sync_with_stdio(false); cin.tie(NULL); ll N; N = 3; vector X(N), Y(N); REP(i, N) cin >> X[i] >> Y[i]; auto [a, b] = ChineseRem(X, Y); if (a == 0) { std::cout << -1 << std::endl; } else { std::cout << a << std::endl; } return 0; }