A,B=map(int, input().split()) C=abs(A-B) a=(B+C)%A b=(A+C)%B c=(C+A)%C if a==0 and b==0 and c==0: print(C) else: print(-1)