#include using namespace std; #include using namespace std; using ll=long long; 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; } // return minimum x s.t. x>=0 and x=b0 (mod m0) and x=b1 (mod m1) // if such x don't exist, return (0,-1) // O(log(m0)+log(m1)) // Note that the risk of overflow pair CRT(ll b0,ll m0,ll b1,ll m1){ ll p,q; ll d = extGcd(m0,m1,p,q); if((b1-b0)%d!=0) return make_pair(ll(0),ll(-1)); ll m = m0/d * m1; ll tmp = (b1-b0)/d * p % (m1/d); ll r = (b0 + m0 * tmp) % m; return make_pair(r,m); } int main(){ vector x(3),y(3); for(int i=0;i<3;i++) cin>>x[i]>>y[i]; auto ret = CRT(x[0],y[0],x[1],y[1]); if(ret.second==-1){ cout<<-1<