#include using namespace std; typedef long long ll; typedef long double ld; typedef pair P; typedef pair Pi; #define rep(i,n) for(ll i=0;i inline bool chmax(T &a, T b){if(a inline bool chmin(T &a, T b){if(a>b){a=b;return true;}return false;} template ostream& operator<<(ostream& s,const complex& d) {return s<<"("< ostream& operator<<(ostream& s,const pair& d) {return s<<"("< ostream& operator<<(ostream& s, const vector& d){int len=d.size();rep(i,len){s< ostream& operator<<(ostream& s,const vector>& d){int len=d.size();rep(i,len){s< ostream& operator<<(ostream& s,const set& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;} template ostream& operator<<(ostream& s,const multiset& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;} template ostream& operator<<(ostream& s,const map& m){s<<"{"< &b,const vector &m){ ll r=0,M=1; rep(i,b.size()){ ll p,q; ll d=extgcd(M,m[i],p,q); if((b[i]-r)%d) return P(0,-1); ll tmp=(b[i]-r)/d*p%(m[i]/d); r+=M*tmp; M*=m[i]/d; } return P((r%M+M)%M,M); } int main(){ cin.tie(0);ios::sync_with_stdio(false); vector b,m; rep(i,3){ ll x,y; cin>>x>>y; b.push_back(x); m.push_back(y); } P ret=CRT(b,m); if(ret.se==-1) cout<<-1<