#include <iostream> #include <string> #include <vector> #include <algorithm> #include <utility> #include <map> #include <set> #include <queue> #include <iomanip> #include <cmath> #include <atcoder/all> using namespace std; using namespace atcoder; typedef long long ll; #define rep(i,n) for (int i = 0; i < int(n);i++) map<ll,ll> dp; ll dfs(ll x,ll a){ if (dp.count(x)) return dp[x]; if (x == 0) return 0; return 2*dfs(x/a,a)+1LL; } int main(){ ll h,a; cin >> h >> a; cout << dfs(h,a) << endl; return 0; }