def f(n, p): val = 0 m = p while n // m: val += n // m m *= p return val N, K, M = map(int, input().split()) print(f(N, M) - f(K, M) - f(N - K, M))