import math

N, D = map(int, input().split())
g = math.gcd(N, D)
print((N // g) - 1)