n = int(input()) prev = [[n, []]] fastest = dict() end_flag = False c = 1 while len(prev) != 0: next = [] for p in prev: if p[0] == 1: print(c) end_flag = True break b = bin(p[0])[2:].count("1") for i in range(2): next_place = p[0] + b * (-1) ** i if next_place not in fastest or fastest[next_place] < c: if 1 <= next_place <= n and not next_place in p[1]: fastest[next_place] = c next.append([next_place, p[1] + [p[0]]]) if end_flag: break c += 1 prev = sorted(next, key = lambda x: x[0], reverse = True) else: print(-1)