import fractions n, d = map(int,input().split()) if n == d: print(0) exit() if d > n / 2: d = n - d if fractions.gcd(n, d) == 1: ans = n - 1 else: if n % d == 0: ans = n // d - 1 else: ans = n // d print(ans)