#include using namespace std; using ll = long long; using P = pair; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 >& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } template string to_string(pair p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef PLOKI_LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // calculate ap + bq = gcd(a, b) // return d = gcd(a, b) ll extgcd(ll a, ll b, ll& p, ll& q) { if(b == 0) { p = 1; q = 0; return a; } ll d = extgcd(b, a % b, q, p); q -= a / b * p; return d; } // solve the equation // * x == r1 (mod. m1) // * x == r2 (mod. m2) // let return val be (r, m), // the solution can be written as // (r, m) == (0, -1) if not exist pair crt(ll r1, ll m1, ll r2, ll m2) { ll p, q; ll d = extgcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d) if((r2 - r1) % d != 0) return {0, -1}; ll m = m1 * (m2 / d); // lcm of (m1, m2) ll tmp = (r2 - r1) / d * p % (m2 / d); ll r = r1 + m1 * tmp; r = (r % m + m) % m; return {r, m}; } int main() { vector x(3), y(3); for (int i = 0; i < 3; ++i) { cin >> x[i] >> y[i]; } auto [xt, yt] = crt(x[0], y[0], x[1], y[1]); if(xt == 0 && yt == -1) { cout << -1 << endl; return 0; } auto [ax, ay] = crt(xt, yt, x[2], y[2]); if(ax == 0 && ay == -1) { cout << -1 << endl; return 0; } cout << ax << endl; }