def pow_mod(a, b, m): ret = 1 while b > 0: if b & 1: ret = ret * a % m a = a * a % m b >>= 1 return ret def gcd(p, q): if q == 0: return p return gcd(q, p % q) def ext_gcd(p, q): # return gcd, (x, y) which satisfies px + qy = 1 if q == 0: return (p, 1, 0) g, y, x = ext_gcd(q, p % q) return (g, x, y - (p // q) * x) def inv(p, q): # return p^(-1) mod q when (p, q) is co-prime g, x, y = ext_gcd(p, q) x %= q if x < 0: x += q return x def garner(X, M, op = 1): # C: (X, Y) M: modulo # list up primes lower than 40000 prime_sup = 30 primes = [] is_prime = [0] * prime_sup for i in xrange(2, prime_sup): if is_prime[i] == 0: primes.append([i, 0]) j = i ** 2 while j < prime_sup: is_prime[j] = i j += i for i in xrange(len(X)): y = X[i][1] for j in xrange(len(primes)): cnt = 0 while (y % primes[j][0] == 0): y /= primes[j][0] cnt += 1 primes[j][1] = max(primes[j][1], cnt) if y > 1: primes.append([y, 1]) for prime in primes[:][:]: if prime[1] == 0: primes.remove(prime) # op = 0 if op == 0: ret = int(1) for i in xrange(len(primes)): ret *= pow_mod(primes[i][0], primes[i][1], M) ret %= M return ret # Does the equation have solutoion? for i in range(len(X)): for j in range(i + 1, len(X)): g = gcd(X[i][1], X[j][1]) if X[i][0] % g != X[j][0] % g: return -1 # reguralization prime_check = [0] * len(primes) for i in xrange(len(X)): for j in xrange(len(primes)): if X[i][1] % pow_mod(primes[j][0], primes[j][1], M) == 0 and prime_check[j] == 0: prime_check[j] = 1 else: while X[i][1] % primes[j][0] == 0: X[i][1] /= primes[j][0] X[i][0] %= X[i][1] X.sort(key = lambda x: (x[1])) # garner's algorithm for i in xrange(len(X)): for j in xrange(i): k = X[i][0] - X[j][0] if k < 0: k += X[i][1] X[i][0] = k * inv(X[j][1], X[i][1]) % X[i][1] ret = int(0) for i in reversed(xrange(len(X))): ret = (ret * X[i][1] + X[i][0]) % M return ret # N = int(input()) Z = [] zero = True mod = int(1e20) for i in xrange(3): Z.append(map(int, raw_input().strip().split(" "))) zero &= (Z[i][0] == 0) if zero == True: print garner(Z, mod, 0) else: print garner(Z, mod, 1)