def getlist(): return list(map(int, input().split())) A, B = getlist() A, B = sorted([A, B]) if B % A != 0: if B % (B - A) == 0 and int(B // (B - A)) == 3: print(B - A) else: print(-1) else: if int(B // A) == 2: print(A * 3) elif int(B // A) == 3: print(A * 2) else: print(-1)