#include #include using namespace std; using ll = long long int; using P = pair; ll myExtGCD(ll a, ll b, ll& x, ll& y){ if(a % b == 0){ x = 0; y = 1; return b; } ll d = myExtGCD(b, a % b, x, y); ll tmp = -y * (a / b) + x; x = y; y = tmp; return d; } P CRT(const vector &b, const vector &mo){ ll r = 0, m = 1; for(int i = 0; i < b.size(); i++){ ll p, q; ll d = myExtGCD(m, mo[i], p, q); if((b[i]-r)%d != 0) return make_pair(0, 0); else{ ll tmp = (b[i]-r)/d * p % (mo[i]/d); r = r + tmp * m; m *= mo[i]/d; while(r < 0) r += m; r %= m; } // cerr << r << " " << m << endl; } return make_pair(r, m); } int main(){ vector x(3), y(3); bool is_allzero = true; for(int i = 0; i < 3; i++){ cin >> x[i] >> y[i]; if(x[i] != 0) is_allzero = false; } P ans = CRT(x, y); if(ans.first == 0 && ans.second == 0) cout << -1 << endl; else if(is_allzero) cout << ans.second << endl; else cout << ans.first << endl; return 0; }