# 辺を張ってBFS or ダイクストラ法で間に合うか N = int(input()) edges = [[] for i in range(N+1)] for i in range(1, N): one_count = bin(i).count('1') if i+one_count <= N: edges[i].append(i+one_count) if i-one_count >= 1: edges[i].append(i-one_count) from collections import deque que = deque() que.append(1) INF = 10**10 distance = [INF]*(N+1) distance[1] = 1 while que: current = que.popleft() for nxt in edges[current]: #print('current', current, 'nxt', nxt) if distance[nxt] > distance[current]+1: distance[nxt] = distance[current]+1 que.append(nxt) if distance[N] == INF: print(-1) else: print(distance[N])