#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include using namespace std; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); ll n,k,m; cin>>n>>k>>m; ll ans=0; ll t=m; while(n/t>=1){ ans+=n/t; if(t>n/m) break; t*=m; } t=m; while(k/t>=1){ ans-=k/t; if(t>k/m) break; t*=m; } t=m; while((n-k)/t>=1){ ans-=(n-k)/t; if(t>(n-k)/m) break; t*=m; } cout<