//#pragma warning(disable:4996) //#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include #include //#include //< in.txt > out.txt using namespace std; //std::ios::sync_with_stdio(false); //std::cin.tie(0); const long long MOD = 1e9 + 7; const long long INF = 1e18; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; //typedef boost::multiprecision::cpp_int bigint; typedef pair PLL; typedef pair PI; typedef pair pdl; typedef pair pdd; typedef vector VLL; typedef vector VVLL; typedef vector VI; typedef vector> VVI; typedef unsigned long long ULL; template using pqueue = priority_queue, function>; template inline void chmin(T& a, T b) { a = min(a, b); } template inline void chmax(T& a, T b) { a = max(a, b); } //y/xのfloorを求める LL floor_(LL y, LL x) { if (x < 0) { x *= -1; y *= -1; } if (y >= 0) { return y / x; } else { if ((-y) % x == 0) { return y / x; } else { return -((-y) / x) - 1; } } } //a mod mを求める inline LL mod(LL a, LL m) { LL res = a % m; if (res < 0) { res += m; } return res; } void input(); void solve(); void daminput(); void naive(); void outputinput(); int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout << fixed << setprecision(12); input(); //daminput(); solve(); //naive(); //outputinput(); return 0; } ////////////////////////////////////////////////// ////////////////////////////////////////////////// //最大公約数 //O(log max(a,b)) template T GCD(T a, T b) { if (b == 0)return a; return GCD(b, (T)(a % b)); } //最大公約数(複数) //O(nlog max(a_i))? template T GCD(vector v) { for (int n = 1; n < v.size(); n++) { v[n] = GCD(v[n], v[n - 1]); } return v[v.size() - 1]; } //最小公倍数 template T LCM(T a, T b) { return a * b / GCD(a, b); } //最小公倍数(複数) template T LCM(vector v) { for (int n = 1; n < v.size(); n++) { v[n] = LCM(v[n], v[n - 1]); } return v[v.size() - 1]; } //ax+by=gcd(a,b)の解 LL extgcd(LL a, LL b, LL& x, LL& y) { LL d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } //ax=gcd(a,m) mod mなるxを返す LL ModInv(LL a, LL m) { LL x, y; extgcd(a, m, x, y); return x; } //x = b1 mod m1 //x = b2 mod m2 //なる連立合同式を解く mod lcm(m1,m2)において解が存在するならば(x,lcm(m1,m2))を、存在しないならば(0,-1)を返す PLL SimCongruence(LL b1, LL m1, LL b2, LL m2) { LL p, q; LL d = extgcd(m1, m2, p, q); if ((b1 - b2) % d != 0) { return PLL(0,-1); } else { LL m = m1 / d * m2; LL tmp = (b2 - b1) / d * p % (m2 / d); LL r = mod(b1 + m1 * tmp, m); return PLL(r, m); } } LL X1, X2, X3; LL Y1, Y2, Y3; void input() { cin >> X1 >> Y1; cin >> X2 >> Y2; cin >> X3 >> Y3; } void daminput() { } void solve() { LL X12, Y12; tie(X12, Y12) = SimCongruence(X1, Y1, X2, Y2); if (Y12 == -1) { cout << -1 << "\n"; return; } X12 = mod(X12, Y12); LL X123, Y123; tie(X123, Y123) = SimCongruence(X12, Y12, X3, Y3); if (Y123 == -1) { cout << -1 << "\n"; return; } else { if (X123 == 0) { cout << Y123 << "\n"; } else { cout << mod(X123, Y123) << "\n"; } } } void naive() { } void outputinput() { }