from collections import deque def bfs(N: int): dist = [-1] * (N + 1) dist[1] = 1 Q = deque([1]) while Q: v = Q.popleft() for d in [-1, 1]: nv = v + d * bin(v).count('1') if (1 <= nv <= N) and dist[nv] == -1: Q.append(nv) dist[nv] = dist[v] + 1 return dist[N] print(bfs(int(input())))