#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; tuple ext_gcd(long long a, long long b){ if (b == 0) return make_tuple(a, 1, 0); long long g, x, y; tie(g, y, x) = ext_gcd(b, a % b); y -= a / b * x; return make_tuple(g, x, y); } pair CRT(vector &b, vector &m){ long long r=0, lcm=1; for (int i=0; i b(3), m(3); for (int i=0; i<3; i++) cin >> b[i] >> m[i]; long long x, l; tie(x, l) = CRT(b, m); cout << (x == 0 ? l : x) << endl; return 0; }