#include using namespace std; // #include // using namespace atcoder; // using mint = modint1000000007; // // using mint = modint998244353; typedef int64_t Int; #define all(x) (x).begin(), (x).end() const double EPS = 1e-10; const Int INF = 1e18; const int inf = 1e9; const Int mod = 1e9+7; //const Int mod = 998244353; template istream &operator>>(istream &is, vector &v) { for (auto &e : v) { is >> e; } return is; } bool print_space_enable = false; void print() { std::cout << '\n'; print_space_enable = false; } template void print(Head&& head, Tail&&... tail) { if (print_space_enable) std::cout << " "; std::cout << fixed << setprecision(15) << head; print_space_enable = true; print(std::forward(tail)...); } template void print(vector v) { for (size_t i = 0; i < v.size(); i++) { if (i > 0) std::cout << " "; std::cout << v[i]; } std::cout << '\n'; } template vector make_vec(size_t n, T val) { return vector(n, val); } template auto make_vec(size_t n, Ts... ts) { return vector(n, make_vec(ts...)); } bool is_overflow(Int a, Int b) { return a > INF / b; } Int f(Int a, Int m) { return (a % m + m) % m; } int64_t extgcd(int64_t a, int64_t b, int64_t &x, int64_t &y) { if (b == 0) { x = 1; y = 0; return a; } int64_t g = extgcd(b, a % b, y, x); y -= a / b * x; return g; } void solve() { Int b[3], m[3]; for (Int i = 0; i < 3; i++) { cin >> b[i] >> m[i]; } Int x = b[0], md = m[0]; for (Int i = 1; i < 3; i++) { Int p, q; Int g = extgcd(md, m[i], p, q); if (x % g != b[i] % g) { print(-1); return; } Int sp = (b[i] - x) / g * p % (m[i] / g); x = f(sp * md + x, lcm(md, m[i])); md = md * (m[i] / g); //print(x % m[i]); } print(x == 0 ? md : x); // for (Int i = 0; i < 3; i++) { // print(x % m[i]); // } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); //Int _t; cin >> _t; while (_t--) solve(); return 0; }