def divisors(n: int) -> list[int]: """n の約数を求める""" s = set() p = 1 while p * p <= n: if n % p == 0: s.add(p) s.add(n // p) p += 1 return sorted(s) A, B = map(int, input().split()) for c in divisors(A+B): if (A+c) % B == 0 and (B+c) % A == 0 and A != c and B != c: print(c) break else: print(-1)