#include const int INF=1<<30; int main(){ long long n,a; std::cin >> n >> a; long long ans=0,count=1; while(n>0){ ans+=count; n/=a; count*=2; } std::cout << ans << '\n' ; }