#include using namespace std; // as + bt = GCD(a,b) a,b:const s,t:var(any) // return GCD(a,b) long long extGCD(long long a, long long b, long long& s, long long& t) { s = 1, t = 0; while(b) { long long tmp = a / b; a -= b * tmp; s -= t * tmp; swap(a, b); swap(s, t); } return a; } // x≡b_i(mod m_i) calc min x,lcm(m_i). if not exist, return // (-1,-1) pair ChineseRem( const vector& b, const vector& m) { long long r = 0, lcm = 1; assert(b.size() == m.size()); long long bsize = b.size(); for(int i = 0; i < bsize; ++i) { long long p, q, d, now; d = extGCD(lcm, m[i], p, q); if((b[i] - r) % d != 0) return make_pair(-1, -1); now = (b[i] - r) / d * p % (m[i] / d); r += lcm * now; lcm *= m[i] / d; } return make_pair((r + lcm) % lcm, lcm); } bool ch = 0; vector x, y; int main() { x.resize(3); y.resize(3); for(int i = 0; i < 3; ++i) { cin >> x[i] >> y[i]; if(x[i]) ch = 1; } pair ans = ChineseRem(x, y); if(!ch) cout << ans.second << endl; else cout << ans.first << endl; return 0; }