import math n, d = map(int, input().split()) print(n // math.gcd(n, d) - 1)