X,Y = list(map(int,input().split()))

for i in range(1,10**6):

    if((X*i) % Y != 0):

        print((X*i))

        exit()

print((-1))

X,Y=list(map(int,input().split()))

print((-1 if X%Y==0 else X))

X, Y = list(map(int, input().split()))
for i in range(1, 10**6):
    if (X * i) % Y != 0:
        print((X * i))
        exit()
print((-1))

X, Y = list(map(int, input().split()))
print((-1 if X % Y == 0 else X))

-for i in range(1, 10**6): -    if (X * i) % Y != 0: -        print((X * i)) -        exit() -print((-1)) +print((-1 if X % Y == 0 else X))