from collections import deque N = input() table = [9999999999] * (N + 1) q = deque() q.append([1,1]) while len(q) > 0: p = q.popleft() current = p[0] cost = p[1] if current == N: print cost exit() next = (current + bin(current).count('1')) if 0 < next and next <= N: if cost + 1 <= table[next]: q.append([next, cost + 1]) table[next] = cost + 1 prev = (current - bin(current).count('1')) if 0 < prev: if cost + 1 <= table[prev]: q.append([current - bin(current).count('1'), cost + 1]) table[prev] = cost + 1 print -1