#include using int64 = long long; using uint64 = unsigned long long; inline int64 mod(int64 a, int64 m) { return (a % m + m) % m; } std::tuple extGCD(int64 a, int64 b) { if (b == 0) return std::make_tuple(1LL, 0LL, a); int64 s, t, g; std::tie(s, t, g) = extGCD(b, a % b); return std::make_tuple(t, s - (a / b) * t, g); } std::tuple CRT(std::vector b, std::vector m) { int n = b.size(); int64 B = 0, M = 1; for (int i = 0; i < n; i++) { int64 p, q, d; std::tie(p, q, d) = extGCD(M, m[i]); if ((b[i] - B) % d != 0) return std::make_tuple(0, false); int64 tmp = mod((b[i] - B) / d * p, m[i] / d); B += M * tmp; M *= m[i] / d; B = mod(B, M); } return std::make_tuple(B, true); } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::vector X(3), Y(3); for (int i = 0; i < 3; i++) { std::cin >> X[i] >> Y[i]; } int64 x; bool ok; std::tie(x, ok) = CRT(X, Y); if (!ok) x = -1; std::cout << x << std::endl; return 0; }