A,B=map(int,input().split()) def f(A,B): for i in range(1,A+B+1): if (A+B)%i: continue if (i+A)%B: continue if (i+B)%A: continue return i return -1 print(f(A,B))