from math import gcd def lcm(a, b): return a // gcd(a, b) * 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 + 1): p = a * y1 + x1 if p % y2 == x2: for b in range(1 if p == 0 else 0, y3 + 1): q = b * lcm12 + p if q % y3 == x3: print(q) return break print(-1) if __name__ == '__main__': solve()