from collections import Counter import heapq def bsf(my_number): if my_number == 1: return 1 my_heap = list() seen = set([1]) heapq.heappush(my_heap, (1, 1)) while len(my_heap) > 0: count, node_id = heapq.heappop(my_heap) seen.add(node_id) crt_ones = get_num_ones(node_id) if node_id + crt_ones == my_number: return count + 1 elif node_id + crt_ones < my_number and (node_id + crt_ones) not in seen: heapq.heappush(my_heap, (count + 1, node_id + crt_ones)) elif node_id - crt_ones > 0 and (node_id - crt_ones) not in seen: heapq.heappush(my_heap, (count + 1, node_id - crt_ones)) return -1 def get_num_ones(my_int): return Counter(str(bin(my_int))[2:]).get('1') if __name__ == '__main__': print(bsf(int(input())))