import heapq N = int(input()) visited = [False] * (N + 1) visited[1] = True queue = [(1, 1)] # (訪問するまでにかかった移動数, 現在のマス) ans = -1 while queue: q = heapq.heappop(queue) moved, now_space = q if now_space == N: ans = moved break next_move = bin(now_space).count("1") forward, backward = now_space + next_move, now_space - next_move if forward < N + 1 and not visited[forward]: visited[forward] = True heapq.heappush(queue, (moved + 1, forward)) if backward > 0 and not visited[backward]: visited[backward] = True heapq.heappush(queue, (moved + 1, backward)) print(ans)