A, B = [int(x) for x in input().split()] A, B = min(A, B), max(A, B) if A == 1 or B == 1: print(0) exit() for i in range(2, 101): if A % i == 0 and B % i == 0: print(-1) exit() x = set() j = 1 while len(x) != B: x.add((j * A) % B) j += 1 ans = 0 for i in range(j * A): if (i % A) % B == 0: pass elif i % A == 0: pass elif i % B == 0: pass elif (i % B) % A == 0: pass else: ans += 1 print(ans // 2)