#include #define all(V) V.begin(),V.end() #define pi 3.1415926535897932384626 #define fi fixed<>; using P = pair; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") template void chmin(T& a,T b){ if(a > b) a=b; } template void chmax(T& a,T b){ if(a < b) a=b; } template void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );} const ll infll=1LL<<62; const ll inf=1<<30; struct Edge{ int to; ll w; Edge(int to,ll w) : to(to),w(w){} }; using Graph = vector>; inline ll mod(ll a, ll m) { return (a % m + m) % m; } //拡張Euclid 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) { ll r = 0, M = 1; rep(i,b.size()) { 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 make_pair(mod(r, M), M); } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); /*----------------------------------------------*/ vector a(3),b(3); bool zero=false; rep(i,3) { cin>>a[i]>>b[i]; if(b[i]) zero=1; } pair mio=ChineseRem(a,b); if (mio.second == -1) cout << -1 << endl; else if(zero) cout << mio.first << endl; else cout << mio.second << endl; return 0; } /*制約を確認しろllじゃなくて大丈夫か?*/