#include using namespace std; int main() { long long N,K,M; cin >> N >> K >> M; long long tmp = M,sum1 = 0,sum2 = 0; while (true) { sum1 += N/tmp-(N-K)/tmp; sum2 += K/tmp; if(1001001001001001001/M > tmp) { tmp *= M; } else { break; } } cout << sum1-sum2 << endl; }