#include<bits/stdc++.h>
using namespace std;
int vis[1000010];
int stepx[2]={-1,1};
struct point{
int x;
int step;
};
queue<point>r;
int main()
{
int y;
cin>>y;
point t;
t.x=1;
t.step=0;
vis[1]=0;
r.push(t);
while(!r.empty()){
int x=r.front().x;
if(x==y){
cout<<r.front().step;
break;
}
for(int i=0;i<=2;i++){
int tx;
if(i<2){
tx=x+stepx[i];
}else{
tx=2*x;
}
if(vis[tx]==0&&tx>0&&tx<=y){
point P;
P.x=tx;
P.step=r.front().step+1;
vis[tx]=1;
r.push(P);
}
}
r.pop();
}
}