from collections import deque N = int(input()) cost = [-1] * (N + 1) cost[1] = 1 que = deque([1]) while que: print(que) a = que.pop() b = bin(a).count('1') if a - b > 0 and cost[a - b] == -1: cost[a - b] = cost[a] + 1 que.append(a - b) if a + b <= N and cost[a + b] == -1: cost[a + b] = cost[a] + 1 que.append(a + b) print(cost[N])