from collections import deque def bc(x): return bin(x).count("1") m=int(input()) st=[-1]*(m+1) st[1]=1 q=deque() q.append(1) while len(q)!=0: cur = q.popleft() curbc=bc(cur) if cur+curbc<=m: if st[cur+curbc]==-1: st[cur+curbc]=st[cur]+1 q.append(cur+curbc) if cur-curbc>=1 and st[cur-curbc]==-1: st[cur-curbc]=st[cur]+1 q.append(cur-curbc) print(st[m])