// // Created by zeronosu77108 on Dec 20, 2020. // #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<ostream &operator<<(ostream &o,const vector&v){o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")< std::tuple ext_gcd(T a, T b) { if (b == 0) return {1, 0, a}; const auto&& [x, y, d] = ext_gcd(b, a%b); return {y, x-a/b*y, d}; } template std::pair, std::optional> crt(const std::vector>& am) { T r = 0, n = 1; for (const auto& [a, m] : am) { const auto&& [x, y, d] = ext_gcd(n, m); // x is inv of n/d (mod. m/d) if ((a - r) % d != 0) return {std::nullopt, std::nullopt}; T tmp = (a - r) / d * x % (m/d); r += n * tmp; n *= m / d; } return {(r%n+n)%n, n}; } int main() { vector> xy; for (int i=0; i<3; i++) { long x,y; cin >> x >> y; xy.emplace_back(x, y); } auto [ans, n] = crt(xy); if (ans) { if (ans.value() == 0) { cout << n.value() << endl; } else { cout << ans.value() << endl; } } else { cout << -1 << endl; } }