import math def extended_gcd(a, b): if b == 0: return (a, 1, 0) else: d, x, y = extended_gcd(b, a % b) return (d, y, x - (a // b) * y) def modinv(a, m): d, x, y = extended_gcd(a, m) if d != 1: return None else: return x % m def crt(a1, m1, a2, m2): d = math.gcd(m1, m2) c = a2 - a1 if c % d != 0: return None m1_prime = m1 // d m2_prime = m2 // d c_prime = c // d inv = modinv(m1_prime, m2_prime) if inv is None: return None k0 = (c_prime * inv) % m2_prime a_result = a1 + k0 * m1 lcm = m1 // d * m2 a_result %= lcm return (a_result, lcm) def main(): x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) x3, y3 = map(int, input().split()) # Merge first two equations res1 = crt(x1, y1, x2, y2) if res1 is None: print(-1) return a, m = res1 # Merge with third equation res2 = crt(a, m, x3, y3) if res2 is None: print(-1) return a_final, m_final = res2 # Calculate the smallest positive solution print(a_final if a_final != 0 else m_final) if __name__ == '__main__': main()