# -*- coding: utf-8 -*- import sys import os import myutil from queue import Queue class Option: val = [] def isNone(v): return isinstance(v, _None) def unwrap(v): return v.val[0] class Some(Option): def __init__(self, v): self.val = [v] def __str__(self): return "Some(" + str(self.val[0]) + ")" class _None(Option): def __init__(self): self.val = [] def __str__(self): return "_None" def lfind(pred, ls): res = list(filter(pred, ls)) if len(res) > 0: return Some(res[0]) else: return _None() def lookup(key, ls): res = lfind(lambda l: l[0] == key, ls) if Option.isNone(res): return _None() else: return Some(Option.unwrap(res)[1]) 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)