from math import gcd a, b = map(int, input().split()) if gcd(a, b) > 1: print(-1) else: seen = set() for i in range(0, a * b + 1, a): for j in range(0, a * b + 1, b): if i + j > a * b: break seen.add(i + j) ans = set(range(a * b + 1)) - seen print(len(ans))