mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline def f(n, m): ret = 0 for i in range(1, 100): x = n // (m ** i) if x == 0: break ret += x return ret N, K, M = map(int, input().split()) print(f(N, M) - f(K, M) - f(N - K, M)) if __name__ == '__main__': main()