#include using namespace std; using ll = long long; using db = double; using ld = long double; template using V = vector; template using VV = vector>; #define fs first #define sc second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define all(v) (v).begin(),(v).end() #define siz(v) (ll)(v).size() #define rep(i,a,n) for(ll i=a;i<(ll)(n);++i) #define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i) #define lb lower_bound #define ub upper_bound #define ENDL '\n' typedef pair Pi; typedef pair PL; const ll mod = 1000000007; const ll INF = 1000000099; const ll LINF = (ll)(1e18 +99); const vector dx={-1,1,0,0},dy={0,0,-1,1}; template inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;} template inline bool chmax(T& t, const U& u){if(t inline T gcd(T a,T b){return b?gcd(b,a%b):a;} template inline T mpow(T a, T n) { T res = 1; for(;n;n>>=1) { if (n & 1) res = res * a; a = a * a; } return res; } 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<