A, B = map(int, input().split()) def make_divisors(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) divisors.sort() return divisors l = make_divisors(A + B) for li in l: if li == A or li == B: continue if (li + A) % B == 0 and (li + B) % A == 0: print(li) break else: print(-1)