A,B = map(int,input().split()) C = set() for i in range(1,A+B+1): if i*i>A+B:break if (A+B)%i==0: C.add(i) C.add((A+B)//i) C = sorted(list(C)) flag = -1 for c in C: if (A+c)%B==0 and (B+c)%A==0 and c!=A and c!=B: flag = c break print(flag)