import queue N=int(input()) def calcpop(num): return bin(num).count("1") def bit(): q=queue.Queue() q.put(1) trout=[-1]*(10**5) trout[1]=1 while q.empty()==False: curpos=q.get() #現在のマス目 popcnt=calcpop(curpos) #現在のマス目を2進数に変換した時の1の数 if(trout[curpos-popcnt]==-1 and curpos-popcnt>0): #後ろのマスに移動するときの判定・処理 trout[curpos-popcnt]=trout[curpos]+1 #移動した先に移動数の最小値を記録 q.put(curpos-popcnt) #このマス目から次は探索を行うためキューに入れる if(trout[curpos+popcnt]==-1 and curpos+popcnt<=N): #前のマスに移動する時の判定・処理 trout[curpos+popcnt] = trout[curpos] + 1 #移動した先に移動数の最小値を記録 q.put(curpos+popcnt) #このマス目から次は探索を行うためキューに入れる print(trout[N]) if __name__ == "__main__": bit()