import math def getlist(): return list(map(int, input().split())) N, D = getlist() GCD = math.gcd(N, D) print(int(N // GCD) - 1)