from functools import lru_cache H, A = map(int, input().split()) from math import ceil, floor import sys sys.setrecursionlimit(10**7) @lru_cache def dfs(num): if num == 0: return 0 elif num == 1: return 1 else: return dfs(num//A)*2+1 print(dfs(H))