#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;cin>>n;vector<int> A(n+1,-1);queue<pair<int,int>> Q;Q.push({1,1});
  while(!Q.empty()){
    auto[x,y]=Q.front();Q.pop();
    if(A[x]==-1){
      A[x]=y;
      if(x==n)break;
      int z=__builtin_popcount(x);
      if(x-z>0)Q.push({x-z,y+1});
      if(x+z<=n)Q.push({x+z,y+1});
    }
  }
  cout<<A[n];
}