# coding=utf-8 x = [0] * 3 y = [0] * 3 ans = [[] for i in range(3)] def xy(y, x): r = [] i = 0 while x * i + y < 10 ** 6: r.append(x * i + y) i += 1 return r for i in range(3): x[i], y[i] = map(int, input().split()) for i in range(3): ans[i] = xy(x[i], y[i]) ans2 = set(ans[0]) & set(ans[1]) & set(ans[2]) if len(ans2) == 0: print('-1') else: print(min(ans2))