XY = [list(map(int,input().split())) for _ in range(3)] X,Y = map(list,zip(*XY)) def extended_euclid(a, b): if b == 0: return (1, 0) else: xd, yd = extended_euclid(b, a % b) return (yd, xd - a // b * yd) from math import gcd,lcm x, y = 0, 1 for i in range(3): x0, y0 = XY[i] d = gcd(y,y0) y //= d y0 //= d p, q = extended_euclid(y, y0) x = (x0 * y * p + x * y0 * q) % (y * y0 * d) y *= y0 * d r = x m = y for x,y in XY: if r % y != x: print(-1) exit() if r == 0: print(lcm(*Y)) else: print(r)