# 拡張ユークリッドの互除法でgcd(a, b) と ax ≡ gcd(a, b) (mod b)となるxを求める # x は a/gcd(a, b) の b/gcd(a,b) を法とした逆元ともいえる。 def gcd_inverse(a, b): a = a % b if a == 0: return b, 0 s, m0 = b, 0 t, m1 = a, 1 while t: n = s // t s, t = t, s - n * t m0, m1 = m1, m0 - n * m1 if m0 < 0: m0 += b // s return s, m0 def crt(b, m): assert len(b) == len(m) n = len(b) b0, m0 = 0, 1 for i in range(n): assert 1 <= m[i] b1 = b[i] % m[i] m1 = m[i] if m0 < m1: m0, m1 = m1, m0 b0, b1 = b1, b0 if m0 % m1 == 0: # m0 = lcm(m0, m1), m1 = gcd(m0, m1) なので b0 ≡ b1 (mod m1) が解が存在する条件であり、b0が解になる if b0 % m1 != b1: return 0, 0 print("r:{}, bi:{}, lcm:{}, x:{}".format(b0, b[i], m0, b0 % m[i])) continue # m0 * p + m1 * q = gcd(m0, m1) d, p = gcd_inverse(m0, m1) q = m1 // d # b0 ≡ b1 (mod gcd(m0, m1)) の不成立 if (b1 - b0) % d: return 0, 0 # x = b0 + (b1 - b0) // g * m0 * p b0 += (b1 - b0) // d * p * m0 # lcm(m0, m1) m0 *= q b0 %= m0 if b0 < 0: b0 += m0 print("r:{}, bi:{}, lcm:{}, x:{}".format(b0, b[i], m0, b0 % m[i])) return b0, m0 x = [0] * 3 y = [0] * 3 for i in range(3): x[i], y[i] = map(int, input().split()) ans, m = crt(x, y) if not m: print(-1) elif ans == 0: print(m) else: print(ans)