a, b = map(int, input().split()) x = max(a, b) y = min(a, b) if x == y * 3: print(y * 2) elif x == y * 2: print(y * 3) elif x * 2 == y * 3: print(y // 2) else: print(-1)