A, B = map(int, input().split()) clist = [] ans = 1000000000000000001 D = int(pow(A + B, 1 / 2)) E = A + B for i in range(1, D + 1): if D % i == 0: clist.append(i) clist.append(int(E / i)) if A in clist: clist.remove(A) if B in clist: clist.remove(B) for i in clist: if A != i and B != i: if (A + i) % B == 0: if (B + i) % A == 0: ans = min(ans, i) if ans == 1000000000000000001: ans = -1 print(ans)