h, a = map(int,input().split()) d = dict() def dp(n): if n == 0: return 0 if n in d: return d[n] ret = 2 * dp(n // a) + 1 d[n] = ret return ret print(dp(h))