def divisor_list(N): #約数のリスト if N == 1: return [1] res = [] for i in range(1,N): if i*i >= N: break if N%i == 0: res.append(i) res.append(N//i) if i*i == N: res.append(i) return sorted(res) a,b = map(int,input().split()) div = divisor_list(a+b) for d in div: if (d+a)%b==0==(d+b)%a and a!=b!=d!=a: print(d) exit() print(-1)