#include using namespace std; using ll = long long; template using V = vector; template using VV = vector>; #define fs first #define sc second #define rep(i,a,n) for(ll i=a;i<(ll)(n);++i) #define ENDL '\n' typedef pair PL; template T extgcd(T a,T b,T &x,T &y){ if(!b){x=1;y=0;return a;} T d=extgcd(b,a%b,y,x); y-=(a/b)*x; return d; } //ax+by=gcd(a,b)を満たすx,y 返り値はgcd template pair ChineseRem(const vector &b,const vector &m){ T r=0,M=1; for(int i=0;i<(int)m.size();++i){ T p,q; T g=extgcd(M,m[i],p,q); if((b[i]-r)%g)return {0,-1}; r+=M*(((b[i]-r)/g*p)%(m[i]/g)); M*=m[i]/g; } return {(r%M+M)%M,M}; } //x=a[i](modm[i]) を満たすx(0<=x<Π(m[i])) を求める //法が互いに素でない場合、解が存在しない場合がある signed main(){ cin.tie(0);ios::sync_with_stdio(false); cout< x(3),y(3); rep(i,0,3){ cin>>x[i]>>y[i]; } PL p=ChineseRem(x,y); if(p.sc==-1)cout<<-1<