import sys sys.setrecursionlimit(10000) goal = int(input()) stepped = dict() maybe_answers = set() def step(position, count): if position in stepped: if count < stepped[position]: stepped[position] = count else: return else: stepped[position] = count if position not in range(1, goal + 1): return if len(maybe_answers) > 0 and count > min(maybe_answers): return if position == goal: maybe_answers.add(count) return position_bin = bin(position) step(position + position_bin.count('1'), count + 1) step(position - position_bin.count('1'), count + 1) step(1, 1) if len(maybe_answers) == 0: print('-1') else: print(min(maybe_answers))