def gcd(x, y): if y == 0: return x return gcd(y,x%y) n, d = list(map(int, input().split())) g = gcd(n,d) ans = n // g - 1 print(ans)