## https://yukicoder.me/problems/no/1809 import math def calc_divisor(K, M): m = M x = K // m ans = x while x > 0: m *= M x = K // m ans += x return ans def main(): N, K, M = map(int, input().split()) # Mを素因数分解 primes_map = {} sqrt_m = int(math.sqrt(M)) for p in range(2, sqrt_m + 1): if M % p == 0: primes_map[p] = 0 while M % p == 0: M //= p primes_map[p] += 1 if M > 1: primes_map[M] = 1 answer = float("inf") for p, v in primes_map.items(): ans1 = calc_divisor(K, p) ans2 = calc_divisor(N - K, p) ans3 = calc_divisor(N, p) x = ans3 - ans2 - ans1 x0 = x // v answer = min(answer, x0) print(answer) if __name__ == "__main__": main()