#include #include #include #include using namespace std; int cnt( int n ) { int b = 0; while( n != 0 ) { if( n & 1 ) b++; n = n >> 1; } return b; } int main() { int N; cin >> N; vector masu( N+1, -1 ); queue q; masu[1] = 1; q.push( 1 ); while( !q.empty() ) { int now = q.front(); int step = cnt( now ); if( now - step >= 1 ) { if( masu[ now -step ] == -1 ) { q.push( now - step ); masu[ now - step ] = masu[now]+1; } } if( now + step <= N ) { if( masu[ now + step ] == -1 ) { q.push( now + step ); masu[ now + step ] = masu[now]+1; } } q.pop(); } cout << masu[N] << endl; return 0; }