// #include "../../merge_src_docs/template/24_template.hpp" #include using namespace std; using ll = long long; #define rep(i, n) for(ll i = 0; i < n; i++) #define rep2(i, l, r) for(ll i = l; i < r; i++) using vi = vector; using vvi = vector; using vll = vector; #define all(A) A.begin(), A.end() #define elif else if using pii = pair; bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; } bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; } struct IOSetup { IOSetup() { cin.tie(0); ios::sync_with_stdio(0); } } iosetup; template void print(vector a) { for(auto x : a) cout << x << ' '; cout << endl; } void print(auto x) { cout << x << endl; } template void print(Head &&head, Tail &&...tail) { cout << head << ' '; print(forward(tail)...); } //#include "../../merge_src_docs/math/crt.hpp" /* - `extgcd(ll a, ll b, ll &x, ll &y)` : `ax + by = gcd(a, b)` を満たす `x, y` を格納する. `|x| + |y|` は最小化されるらしい - `pair crt(vll &r, vll &m)` : `x = r[i] (mod m[i])` を解く. 解が存在しないなら `{-1, -1}`, 存在するなら `{x, lcm(m[1], m[2], ...)}` を返す. */ ll extgcd(ll a, ll b, ll &x, ll &y) { // assert(a >= 0 && b >= 0); if(!b) return x = 1, y = 0, a; ll d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } ll inv_mod(ll x, ll m) { ll y, z; extgcd(x, m, y, z); return (y % m + m) % m; } pair crt(vll r, vll m) { int n = r.size(); ll r0 = 0, m0 = 1; rep(i, n) { ll g, im, x; g = extgcd(m0, m[i], im, x); if((r[i] - r0) % g) return {-1, -1}; ll tmp = (r[i] - r0) / g * im % (m[i] / g); r0 += m0 * tmp; m0 *= m[i] / g; } return {(r0 % m0 + m0) % m0, m0}; } // https://yukicoder.me/problems/447 void solve1() { vector X(3), Y(3); rep(i, 3) { ll x, y; cin >> x >> y; X[i] = x, Y[i] = y; } auto [r, m] = crt(X, Y); if(r == 0) r = m; cout << r << '\n'; return; } int main() { solve1(); return 0; }