#include #include #include #include #include long long mod_inv(long long a, int m) { if ((a %= m) < 0) a += m; if (std::__gcd(a, static_cast(m)) != 1) return -1; long long b = m, x = 1, u = 0; while (b > 0) { long long q = a / b; std::swap(a -= q * b, b); std::swap(x -= q * u, u); } x %= m; return x < 0 ? x + m : x; } template std::pair simultaneous_linear_congruence(const std::vector &a, const std::vector &b, const std::vector &m) { const int n = a.size(); T x = 0, md = 1; for (int i = 0; i < n; ++i) { T l = md * a[i], r = -x * a[i] + b[i], g = std::__gcd(l, m[i]); if (r % g != 0) return {0, -1}; x += md * (r / g * mod_inv(l / g, m[i] / g) % (m[i] / g)); md *= m[i] / g; } return {x < 0 ? x + md : x, md}; } int main() { constexpr int N = 3; std::vector x(N), y(N); for (int i = 0; i < N; ++i) { std::cin >> x[i] >> y[i]; } long long ans, mod; std::tie(ans, mod) = simultaneous_linear_congruence(std::vector(N, 1), x, y); if (mod == 0) { std::cout << "-1\n"; } else { std::cout << (ans == 0 ? mod : ans) << '\n'; } return 0; }