# coding: utf-8 from collections import deque n = int(input()) v = [0 for i in range(n + 1)] q = deque() q.append(1) v[1] = 1 result = -1 while(q): u = q.popleft() if u == n: result = v[u] break steps = str(format(u, 'b')).count('1') front = u + steps back = u - steps if (1 <= front <= n) and v[front] == 0: q.append(front) v[front] = v[u] + 1 if (1<= back <= n) and v[back] == 0: q.append(back) v[back] = v[u] + 1 print(result)