#include using namespace std; using LL = long long int; #define incID(i, l, r) for(int i = (l) ; i < (r); ++i) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i) #define incII(i, l, r) for(int i = (l) ; i <= (r); ++i) #define decII(i, l, r) for(int i = (r) ; i >= (l); --i) #define inc(i, n) incID(i, 0, n) #define dec(i, n) decID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec1(i, n) decII(i, 1, n) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define FR front() #define BA back() #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() auto setmin = [](auto & a, auto b) { return (b < a ? a = b, true : false); }; auto setmax = [](auto & a, auto b) { return (b > a ? a = b, true : false); }; auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); }; auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); }; #define SI(v) static_cast(v.size()) #define RF(e, v) for(auto & e: v) #define until(e) while(! (e)) #define if_not(e) if(! (e)) #define ef else if #define UR assert(false) #define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__); void IN_() { }; template void IN_(T & a, U & ... b) { cin >> a; IN_(b ...); }; template void OUT(T && a) { cout << a << endl; } template void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); } // ---- ---- template T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } auto fl = [](auto a, auto b) { assert(b != 0); return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0); }; auto ce = [](auto a, auto b) { assert(b != 0); return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0); }; auto mo = [](auto a, auto b) { assert(b != 0); a %= b; if(a < 0) { a += abs(b); } return a; }; template class CRT { private: T A, M; bool ok, positive; array ext_gcd(T a, T b) { if(b == 0) { return { 1, 0, a }; } auto x = ext_gcd(b, a % b); return { x[1], x[0] - (a / b) * x[1], x[2] }; } T multi(T a, T b, T m) { // a * b % m a %= m; b %= m; T B = 1'000'000'000; T au = a / B, ad = a % B; T bu = b / B, bd = b % B; T s0 = ad * bd % m; T s1 = (au * bd + ad * bu) % m; T s2 = au * bu % m; inc(i, 9) { (s1 *= 10) %= m; } inc(i, 18) { (s2 *= 10) %= m; } return (s0 + s1 + s2) % m; } public: CRT(bool p = false) { A = 0; M = 1; ok = true; positive = p; } void add(T a, T m) { assert(inID(a, 0, m)); if(! ok) { return; } auto qq = ext_gcd(M, -m); assert(M * qq[0] - m * qq[1] == qq[2]); if((a - A) % qq[2] != 0) { ok = false; return; } T d = (a - A) / qq[2]; T L = lcm(M, m); T Q = multi(qq[0], d, m); T q = multi(qq[1], d, M); T X = M * Q + A; T x = m * q + a; assert(X == x); if(x < 0) { Q += ce(-X, L) * L / M; q += ce(-x, L) * L / m; X = M * Q + A; x = m * q + a; } assert(Q >= 0 && q >= 0); assert(M * Q + A == m * q + a); A = M * Q + A; M = L; assert(inID(A, 0, M)); } optional get_a() { optional a; if(ok) { a = (positive && A == 0 ? M : A); } return a; } T get_m() { return M; } }; int main() { CRT crt(true); inc(i, 3) { IN(LL, a, m); crt.add(a, m); } OUT(crt.get_a().value_or(-1)); }