#include std::pair crt(std::vector &a, std::vector &m){ assert(a.size() == m.size()); long long ax = a[0], mx = m[0]; auto extgcd = [](long long r, long long s) -> std::tuple{ bool rev = false; if(s < r){ std::swap(r, s); rev = true; } long long a = 1, b = 0, c = 0, d = 1, k; while(s){ k = r / s; a -= k * c; b -= k * d; std::swap(a, c); std::swap(b, d); r %= s; std::swap(r, s); } if(rev) return std::make_tuple(b, a, r); else return std::make_tuple(a, b, r); }; for(int i = 1, sz = a.size(); i < sz; i++){ long long my = m[i]; long long ay = (a[i] % my + my) % my; auto [rx, ry, g] = extgcd(mx, my); if((ay - ax) % g != 0) return std::make_pair(0, 0); long long l = my / g; ax = ax + ((ay - ax) / g) % l * rx % l * mx; mx *= l; if(ax < 0) ax += mx; } return std::make_pair(ax, mx); } int main(){ std::vector a(3, 0LL), b(3, 0LL); for(int i = 0; i < 3; i++) std::cin >> a[i] >> b[i]; auto res = crt(a, b); if(res.second == 0) res.first = -1; std::cout << res.first << "\n"; }