#include using namespace std; using ll = long long; #define MOD int(1e9+7) #define INF int(1e9+7) #define LINF ll(1e18+7) #define PI acos(-1) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define P pair #define chmax(x,y) (x = max(x,y)) #define chmin(x,y) (x = min(x,y)) ll ext_gcd(ll a, ll b, ll &x, ll &y){ if(b==0){ x=1; y=0; return a; } ll d=ext_gcd(b,a%b,y,x); y -= a/b*x; return d; } P crt(const vector &b, const vector &m){ ll r=0,M=1; rep(i,(int)b.size()){ ll p,q; ll d = ext_gcd(M,m[i],p,q); if((b[i]-r)%d != 0) return P(0,0); ll tmp = (b[i]-r)/d * p %(m[i]/d); r += M * tmp; M *= m[i]/d; } return P((r+M)%M,M); } // int main(){ vector b(3),m(3); rep(i,3) cin>>b[i]>>m[i]; ll r,mod; tie(r,mod) = crt(b,m); if(P(r,mod)==P(0,0)) cout<<-1<