# -*- coding: utf-8 -*- import sys import os from queue import Queue def rangeOfCell(n, acc): if n==0: return acc else: return rangeOfCell(n//2, acc+(n%2)) def neighborsOf(memo, i): res = [] for j in range(0, len(memo)): r = memo[j][0] if abs(i-j) == r and memo[j][1] == -1: res += [j] return res def bfs(memo): q = Queue() goal = len(memo) - 1 q.put(goal) while not q.empty(): i = q.get() if i == 0: return memo[i][1] else: count = memo[i][1] neighbors = neighborsOf(memo, i) for n in neighbors: memo[n] = (memo[n][0], count+1) q.put(n) return -1 def solve(inputs): memo = [(rangeOfCell(i+1, 0), 1) if i == inputs - 1 else (rangeOfCell(i+1, 0), -1) for i in range(0, inputs)] print(bfs(memo)) inputs = int(input()) solve(inputs)