import sys sys.setrecursionlimit(10000) goal = int(input()) stepped = dict() maybe_answer = -1 def step(position, count): global maybe_answer # 現在地点が範囲外 if position not in range(1, goal + 1): return # 既に到達した位置か? if position in stepped: if count < stepped[position]: # 既出の到達経路より短い場合は、到達経路を更新 stepped[position] = count else: # 既出の到達経路と同じ、または大きい場合は処理終了 return else: stepped[position] = count # 既出のゴールまでの最短経路を超えてしまった if maybe_answer != -1 and count > maybe_answer: return # ゴールに到達したので処理終了 if position == goal: maybe_answer = count return # 現在位置から進む・戻るを検証 position_1bit_num = bin(position).count('1') step(position + position_1bit_num, count + 1) step(position - position_1bit_num, count + 1) step(1, 1) print(maybe_answer)