import numpy as np N = int(input()) lengths = [bin(i).count('1') for i in range(1, N+1)] g = [] for i, length in enumerate(lengths): backward = i - length forward = i + length edges = [] if not backward < 0: edges.append(backward) if not forward > N-1: edges.append(forward) g.append(edges) distances = np.full_like(lengths, -1) que = [] distances[0] = 1 que.append(0) while que!=[]: v = que.pop(0) for next_v in g[v]: if distances[next_v]!=-1: continue distances[next_v] = distances[v] + 1 que.append(next_v) print(distances[N-1])