from sys import stdin def primeFactors(n): factors = [] apnd = factors.append while n >= 4: if n % 2: break else: n //= 2 apnd(2) i = 3 while n >= i * i: if n % i: i += 2 else: n //= i apnd(i) apnd(n) return factors def main(): A, B = map(int, input().split()) if A == B: print(-1) return p = primeFactors(A + B) if len(p) == 1: print(-1) else: for c in p: if not (c + A) % B and not (c + B) % A: print(c) break else: print(-1) input = lambda: stdin.readline().rstrip() main()