#!/usr/bin/env python3 from collections import deque def memoize(f): table = {} def func(*args): if not args in table: table[args] = f(*args) return table[args] return func @memoize def nexts(node): r = format(node, "b").count('1') return [i + node for i in (-r, r) if 0 < i + node <= N] N = int(input()) node = 1 pend = deque() hop = {1: 1} while node < N: nxts = [n for n in nexts(node) if n not in hop.keys()] pend.extend(nxts) for n in nxts: hop[n] = hop[node] + 1 if len(pend) == 0: print(-1) exit() node = pend.popleft() print(hop[N])