#include using namespace std; using ll = long long; using P = pair; using T = tuple; // #include // using namespace atcoder; // using mint = modint1000000007; #define rep(i, n) for(ll i = 0; i < n; i++) int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); ll h,a; cin >> h >> a; map memo; auto dfs = [&](auto self, ll now) -> ll { if( now == 0 ) return 0ll; if( memo.count(now) ) return memo[now]; memo[now]++; memo[now] += self(self,now/a) * 2; return memo[now]; }; dfs(dfs,h); cout << memo[h] << endl; return 0; }