import math def divisors(n): lst = [] for i in range(1, int(math.sqrt(n)) + 1): if n % i == 0: lst.append(i) if i != n // i: lst.append(n // i) lst.sort() return lst A, B = map(int, input().split()) for d in filter(lambda x: x != A and x != B, divisors(A + B)): if (A + d) % B == 0 and (B + d) % A == 0: print(d) break else: print(-1)