a,b=map(int,input().split()) n=a+b l=[] i=1 while i*i<=n: if n%i<1: l+=[i,n//i] i+=1 c=-1 for i in l: if (a+i)%b==(b+i)%a==0: c=i; break print(c)