# return (g, x, y) which satisfies ax + by = g and g = GCD(a,b) def ext_gcd(a: int, b: int) -> tuple[int, int, int]: if b: g, y, x = ext_gcd(b, a % b) y -= (a // b) * x return g, x, y return a, 1, 0 # return (r, m) which satisfies x \equiv r (mod m) as solution # when no solution ( (b1-b2) % gcd(m1, m2) != 0 ), return(0, -1) def crt(m1: int, m2: int, b1: int, b2: int) -> tuple[int, int]: g, p, q = ext_gcd(m1, m2) if (b2 - b1) % g != 0: return 0, -1 m = m1 * m2 // g s = (b2 - b1) // g r = b1 + s * m1 * p r %= m return r, m # return (r, m) which satisfies x \equiv r (mod m) as solution # when no solution ( (b1-b2) % gcd(m1, m2) != 0 ), return(0, -1) def crt_n(ms: list[int], bs: list[int]) -> tuple[int, int]: r, m = 0, 1 for bi, mi in zip(bs, ms): g, p, q = ext_gcd(m, mi) if (bi - m) % g != 0: return 0, -1 s = (bi - r) // g r += s * m * p r %= m m *= mi // g return r, m bs = [] ms = [] for i in range(3): bi, mi = map(int, input().split()) bs.append(bi) ms.append(mi) r, m = crt_n(ms, bs) print(m)