from collections import deque n = int(input()) steps = [None, 1] visited = [None, False] for _ in range(2, n+1): steps.append(-1) visited.append(False) q = deque([]) start = 1 q.append(start) while q: v = q.popleft() visited[v] = True if v == n: # print("goal") break num = bin(v).count('1') for next_v in [v + num, v - num]: if 1 <= next_v and next_v <= n: if visited[next_v] == False: q.append(next_v) if steps[next_v] == -1: steps[next_v] = steps[v] + 1 else: steps[next_v] = min(steps[next_v], steps[v] + 1) print(steps[n])