#include using namespace std; typedef long long ll; constexpr int Inf = 1000000030; constexpr ll INF= 2000000000000000001; constexpr ll MOD = 998244353; const double PI = 3.1415926535897; typedef pair P; typedef pair PP; template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll mod(ll val, ll M) { val = val % M; if(val < 0) { val += M; } return val; } template T RS(T N, T P, T M){ if(P == 0) { return 1; } if(P < 0) { return 0; } if(P % 2 == 0){ ll t = RS(N, P/2, M); if(M == -1) return t * t; return t * t % M; } if(M == -1) { return N * RS(N,P - 1,M); } return N * RS(N, P-1, M) % M; } //extgcd(a,b,x,y):ax+by=gcd(a,b)を満たす(x,y)が格納される 返り値はgcd(a,b) ll extgcd(ll a,ll b,ll &x,ll &y) { if(b == 0) { x = 1; y = 0; return a; } ll d = extgcd(b,a % b,y,x); y -= a / b * x; return d; } //CRT(b,m):n次合同方程式を解く。答えは x = retr(mod retm)の形で表される(解が無い場合は(0,-1)の形で表される) pair CRT(vector b,vector m) { ll retr = 0; ll retm = 1; for(int i = 0;i < (int)b.size();i++) { ll x,y; ll d = extgcd(retm,m.at(i),x,y); if((b.at(i) - retr) % d != 0) return make_pair(0,-1); ll tmp = (b.at(i) - retr) / d * x % (m.at(i) / d); retr += retm * tmp; retm *= m.at(i) / d; } return make_pair(mod(retr,retm),retm); } int main() { ll X1,Y1,X2,Y2,X3,Y3; cin >> X1 >> Y1 >> X2 >> Y2 >> X3 >> Y3; pair ret = CRT({X1,X2,X3},{Y1,Y2,Y3}); if(ret.second == -1) cout << -1 << endl; else cout << ret.first << endl; }