#include using namespace std; namespace CRT{ long long extGCD(long long a, long long b, long long &x, long long &y){ if(b == 0){ x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } pair ChineseRem(const vector &b, const vector &m) { long long r = 0, M = 1; for(int i = 0; i < (int) b.size(); i++){ long long p, q; long long d = extGCD(M, m[i], p, q); if((b[i] - r) % d != 0) return {0, -1}; long long tmp = (b[i] - r) / d * p % (m[i] / d); r += M * tmp; M *= m[i] / d; } r %= M; if(r < 0) r += M; return {r, M}; } } int main(){ vector b(3), m(3); int cnt = 0; for(int i = 0; i < 3; i++){ cin >> b[i] >> m[i]; if(b[i] == 0) cnt++; } pair p = CRT::ChineseRem(b, m); if(p.second == -1){ cout << -1 << endl; }else if(cnt == 3){ cout << p.second << endl; }else{ cout << p.first << endl; } }