def search(N): history = set([]) queue = [0] res = -1 time = 1 while queue != []: next_queue = [] for pos in queue: if pos == N-1: res = time break else: history.add(pos) val = pos+1 bitnum = bin(val).count('1') posR = pos + bitnum posL = pos - bitnum if posR < N and not posR in history: next_queue.append(posR) if posL > 0 and not posL in history: next_queue.append(posL) time += 1 queue = next_queue return res def main(): N = int(input()) res = search(N) print(res) main()