x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) x3, y3 = map(int, input().split()) for i in range(1, 100001): j = 0 j += 1 if i % y1 == x1 else 0 j += 1 if i % y2 == x2 else 0 j += 1 if i % y3 == x3 else 0 if j == 3: print(i) break else: print(-1)