import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from collections import deque N = int(read()) INF = 10 ** 9 dist = [INF] * (N+1) dist[1] = 0 q = deque([1]) while q: v = q.popleft() d = str(bin(v)).count('1') for y in [v-d,v+d]: if not(1<=y<=N): continue if dist[y] != INF: continue q.append(y) dist[y] = dist[v] + 1 answer = dist[-1] + 1 if answer > INF: answer = -1 print(answer)