def extgcd(a, b): if b == 0: return (1, 0, a) X, Y, g = extgcd(b, a % b) return (Y, X - (a // b) * Y, g) def chineserem(B, M): r = 0 ALLM = 1 for i in range(len(B)): x, y, g = extgcd(ALLM, M[i]) if (B[i] - r) % g != 0: return (-1, -1) r += ((((B[i] - r) // g) * x) % (M[i] // g)) * ALLM ALLM *= (M[i] // g) return (r % ALLM, ALLM) B = [] M = [] for i in range(3): x, y = map(int, input().split()) B.append(x) M.append(y) print(chineserem(B, M)[0])