import queue #移動するマス数を求める関数 def calcpop(num): return bin(num).count("1") N=int(input()) q=queue.Queue() q.put(1) masu =[] masu=[-1]*10000 masu[1]=1 while q.empty()==False: curpos=q.get() popcnt=calcpop(curpos) if (curpos-popcnt>-1 and masu[curpos-popcnt]==-1): q.put(curpos-popcnt) if (curpos+popcnt<=N and masu[curpos+popcnt]==-1): masu[curpos+popcnt]=masu[curpos]+1 q.put(curpos+popcnt) print(masu[N])