#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long MOD = 1000000007; int bitcount( int a ) { int ret = 0; for ( int i = 0; (1 << i ) <= a; i++ ) { if ((1<> N; vector< vector > path(N+1); vector dist(N+1); fill( dist.begin(), dist.end(), -1 ); dist[1] = 1; for ( int i = 1; i <= N; i++ ) { int b = bitcount(i); if ( i+b <= N ) { path[i].push_back(i+b); } if ( i-b >= 1 ) { path[i].push_back(i-b); } } queue Q; for ( int i = 0; i < path[1].size(); i++ ) { Q.push( path[1][i] ); dist[path[1][i]] = dist[1]+1; } while ( !Q.empty() ) { int c = Q.front(); Q.pop(); if ( c == N ) { break; } for ( int i = 0; i < path[c].size(); i++ ) { if ( dist[path[c][i]] == -1 ) { Q.push( path[c][i] ); dist[path[c][i]] = dist[c]+1; // cout << dist[path[c][i]] << endl; } } } cout << dist[N] << endl; return 0; }