def popcount(x): ret = 0 while x > 0: ret += x & 1 x //= 2 return ret N = int(input()) dist = [-1] * (N + 1) dist[1] = 1 from collections import deque qu = deque([1]) while len(qu): v = qu.popleft() d = popcount(v) if 1 <= v - d <= N and dist[v - d] == -1: qu.append(v - d) dist[v - d] = dist[v] + 1 if 1 <= v + d <= N and dist[v + d] == -1: qu.append(v + d) dist[v + d] = dist[v] + 1 print(dist[N])