from collections import deque
N = int(input())

def bfs(N: int) -> int:
    dist = [-1] * (N + 1) # 各マスへの最短距離リスト
    dist[1] = 1

    que = deque([1])
    while que:
        now_p = que.popleft() # queから取り出す
        for p in [-1, 1]: # 進む/戻る
            next_p = now_p + p * bin(now_p).count('1')
            if next_p == N:
                return dist[now_p] + 1

            elif (1 <= next_p <= N) and dist[next_p] == -1:
                que.append(next_p)
                dist[next_p] = dist[now_p] + 1
    return dist[N]

print(bfs(N))