def gcd(a, b): if b == 0: return a else: return gcd(b, a%b) def extgcd(a, b): #拡張ユークリッド互除法 #ax + by = gcd(a, b)となる(x, y)を求めます if b == 0: return [1, 0] q = a//b r = a%b s, t = extgcd(b, r) y = s - q*t return [t, y] def CRT(rs, ms): R = 0 M = 1 for r, m in zip(rs, ms): g = gcd(M, m) # assert r%g == R%g if r%g != R%g: return [-1, -1] s, _ = extgcd(M, m) lcm = m*M // g R += (r - R)//g * M * s R %= lcm M = lcm return [R, M] x = [] y = [] for i in range(3): X, Y = map(int, input().split()) x.append(X) y.append(Y) print(CRT(x, y)[0])