/*    ∫ ∫ ∫    ノヽ   (_  )  (_    ) (______ )  ヽ(´・ω・)ノ     |  /    UU */ #pragma region macro #include typedef long long int64; using namespace std; using P = pair; typedef vector vi; const int MOD = (int)1e9 + 7; const int64 INF = 1LL << 62; const int inf = 1<<30; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b ostream& operator<<(ostream& os, vector &V){ int N = V.size(); REP(i,N){ os << V[i]; if (i!=N-1) os << " "; } os << "\n"; return os; } template ostream& operator<<(ostream& os, pair const&P){ os << "("; os << P.first; os << " , "; os << P.second; os << ")"; return os; } template ostream& operator<<(ostream& os, set &S){ auto it=S.begin(); while(it!=S.end()){ os << *it; os << " "; it++; } os << "\n"; return os; } template ostream& operator<<(ostream& os, deque &q){ for(auto it=q.begin();it> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)}; #pragma endregion //fixed< bit; for(b=b;b>0;b>>=1){ bit.push_back(b&1); } vector fac(bit.size()); fac[0] = a; int64 res = 1; for(int i=1;i= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) { if ((x += MOD-a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(int64 t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD-2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res/=a; } }; ostream& operator<<(ostream& os, mint a){ os << a.x; return os; } //拡張Euclidの互除法 //ap + bq = gcd(a,b) となる(p,q)を求め、gcd(a,b)を返す long long exGCD(long long a, long long b, long long &p, long long &q){ if(b==0){ p=1; q= 0; return a; } long long d = exGCD(b,a%b,q,p); q -= a/b * p; return d; } //中国の剰余定理 //解がx≡r(mod. M)となるような r,Mをpairで返す pair ChineseRem(const vector& b,const vector& m){ if(b.size()!=m.size()){ cerr << "bとmのサイズが違います" << bn; return make_pair(-1,-1); } long long r = 0, M = 1; long long p,q; REP(i,b.size()){ long long d = exGCD(M,m[i],p,q); if((b[i]-r)%d != 0) return make_pair(-1,-1); //解無し long long tmp = (b[i] - r)/d * p % (m[i]/d); r += M * tmp; M *= m[i] / d; } r%=M; while(r<0) r+=M; return make_pair(r,M); } int main(){ cin.tie(0); ios::sync_with_stdio(false); vector X(3),Y(3); REP(i,3){ cin >> X[i] >> Y[i]; } int64 ans,M; tie(ans,M) = ChineseRem(X,Y); if(ans==0) ans+=M; cout << ans << endl; }