#include #if __has_include() #include std::ostream &operator<<(std::ostream &os, const atcoder::modint998244353 &v) { os << v.val(); return os; } std::istream &operator>>(std::istream &is, atcoder::modint998244353 &v) { long long x; is >> x; v = x; return is; } std::ostream &operator<<(std::ostream &os, const atcoder::modint1000000007 &v) { os << v.val(); return os; } std::istream &operator>>(std::istream &is, atcoder::modint1000000007 &v) { long long x; is >> x; v = x; return is; } #endif using namespace std; using ll = long long; using pll = pair; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--) #define all(x) begin(x), end(x) #define TT template TT using vec = vector; TT using vvec = vec>; TT using vvvec = vec>; TT using minheap = priority_queue, greater>; TT using maxheap = priority_queue; template bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; } template bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; template ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } TT ostream &operator<<(ostream &os, const vec &v) { for (size_t i = 0; i < v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template ostream &operator<<(ostream &os, const array &v) { for (size_t i = 0; i < n; i++) { os << v[i] << (i + 1 != n ? " " : ""); } return os; } template std::ostream &operator<<(ostream &os, const vvec &v) { for (size_t i = 0; i < v.size(); i++) { os << v[i] << (i + 1 != v.size() ? "\n" : ""); } return os; } TT istream &operator>>(istream &is, vec &v) { for (size_t i = 0; i < v.size(); i++) { is >> v[i]; } return is; } #if __has_include() #include #else #define dbg(...) true #define DBG(...) true #endif namespace CRT { template T extgcd(T a, T b, T &x, T &y) { if (b == 0) { x = 1; y = 0; return a; } T d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } template T modinv(T a, T MOD) { T x, y; extgcd(a, MOD, x, y); return (x % MOD + MOD) % MOD; } template bool is_feasable(T x0, T m0, T x1, T m1) { T g = gcd(m0, m1); return x0 % g == x1 % g; } template pair crt_2(T x0, T m0, T x1, T m1) { if(m0 == 0 || m1 == 0) { return make_pair(0, 0); } if(x0 < 0 || x0 >= m0) { x0 %= m0; if(x0 < 0) x0 += m0; } if(x1 < 0 || x1 >= m1) { x1 %= m1; if(x1 < 0) x1 += m1; } if(m0 < m1) { swap(x0, x1); swap(m0, m1); } T a, b; T g = extgcd(m0, m1, a, b); if (x0 % g != x1 % g) { return make_pair(0, 0); } T lc = lcm(m0, m1); T u0 = m0 / g, u1 = m1 / g; T x = (x1 - x0) / g * modinv(u0, u1) % u1; if (x < 0) x += u1; x = x * m0 + x0; return make_pair(x, lc); } }; // namespace CRT int main() { vec xs(3), ys(3); rep(i, 0, 3) { cin >> xs[i] >> ys[i]; } ll c = 0, m = 1; rep(i, 0, 3) { auto [new_c, new_m] = CRT::crt_2(c, m, xs[i], ys[i]); c = new_c; m = new_m; } if(m == 0) { cout << -1 << endl; return 0; } if(c == 0) { c += m; } cout << c << endl; }