#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; template pair ext_gcd(T a, T b) { if (b == 0) return {1, 0}; T fst, snd; tie(fst, snd) = ext_gcd(b, a % b); return {snd, fst - a / b * snd}; } template pair crt(const vector &b, const vector &m) { T x = 0, md = 1; int n = b.size(); REP(i, n) { T g = __gcd(md, m[i]); if ((b[i] - x) % g != 0) return {0, -1}; x += md * ((b[i] - x) / g * ext_gcd(md, m[i]).first % (m[i] / g)); md *= m[i] / g; } return {x < 0 ? x + md : x, md}; } int main() { const int N = 3; vector x(N), y(N); REP(i, N) cin >> x[i] >> y[i]; pair ans = crt(x, y); cout << (ans.second == -1 ? -1 : ans.first) << '\n'; return 0; }