A,B=map(int,input().split()) a,b=A,B if A>B else (B,A) if a*2==b*3: c=b//2 elif a==b*3: c=b*2 elif a==b*2: c=b*3 else : c=-1 print(c)