def divisors(N): res = [] for i in range(1, int(N ** 0.5) + 1): if N % i == 0: res.append(i) if i * i != N: res.append(N // i) res.sort() return res A, B = map(int, input().split()) D = divisors(A + B) for C in D: if (A + C) % B == 0 and (B + C) % A == 0: print(C) exit() print(-1)