A,B=map(int,input().split()) x=A+B import math xr=math.ceil(math.sqrt(x)) LIST=[] for i in range(1,xr+1): if x%i==0: LIST.append(i) LIST.append(x//i) LIST.sort() for l in LIST: if (A+l)%B==0 and (B+l)%A==0 and A!=l and B!=l and A!=B: print(l) break else: print(-1)