def gcd(a, b): m, n = min(a, b), max(a, b) while n != 0: m, n = n, m % n return m def lcm(a, b): return a * b // gcd(a, b) def solve(): x1, y1 = list(map(int, input().split())) x2, y2 = list(map(int, input().split())) x3, y3 = list(map(int, input().split())) lcm12 = lcm(y1, y2) for a in range(y2): p = a * y1 + x1 if p % y2 == x2: for b in range(y3): q = b * lcm12 + p if q % y3 == x3: print(q) return print(-1) if __name__ == '__main__': solve()