#include #include #include using namespace std; /* #include using namespace atcoder; using mint = modint1000000007; */ #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define endl "\n" #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") typedef long long ll; typedef pair pii; typedef pair pll; template ostream &operator<<(ostream &os, const vector &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;} template ostream &operator<<(ostream &os, const pair &p) {os << "(" << p.first << ", " << p.second << ")"; return os;} ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } const int mod = 1e9 + 7; map dp; ll f(ll x, ll A) { if (x <= 0) return dp[x] = 0; if (dp.find(x) != dp.end()) return dp[x]; ll y = x / A; ll ret = f(x / A, A); return dp[x] = ret * 2 + 1; } void solve() { ll H, A; cin >> H >> A; cout << f(H, A) << endl; } int main() { #ifdef LOCAL_ENV cin.exceptions(ios::failbit); #endif cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); }