#pragma GCC optimize("O3") #include #define ll long long using namespace std; template struct CRT { vector rem, mod; void add(T r, T m) { rem.emplace_back(r); mod.emplace_back(m); } pair calc() { T r = 0, m = 1; for (int i=0; i<(int)rem.size(); i++) { auto ext_gcd = [](T a, T b, T &x, T &y, T &d) -> void { x = 1, y = 0; T u = 0, v = 1; while (b) { T p = a/b, q = a%b, r = x-u*p; x = u, u = r; r = y-v*p; y = v, v = r; a = b, b = q; } d = a; }; T p, q, d; ext_gcd(m, mod[i], p, q, d); if ((rem[i]-r)%d != 0) return {0, 0}; T t = (rem[i]-r)/d*p%(mod[i]/d); r += m*t; m *= mod[i]/d; } return {r<0?r+m:r, m}; } }; void solve() { CRT crt; for (int i=0; i<3; i++) { ll x, y; cin >> x >> y; crt.add(x, y); } auto [r, m] = crt.calc(); cout << (m?r?r:m:-1) << '\n'; } signed main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); solve(); return 0; }