n=int(input()) g=[[] for _ in range(n+1)] for i in range(1,n): m=bin(i).count("1") if i+m<=n:g[i].append(i+m) if 1<=i-m:g[i].append(i-m) from collections import deque seen=[-1]*(n+1) seen[1]=1 todo=deque([1]) while todo: v=todo.popleft() for nv in g[v]: if seen[nv]==-1: seen[nv]=seen[v]+1 todo.append(nv) print(seen[n])