#include #include using namespace std; int count_bit(int x); int main() { queue a; int N; int now = 1; cin >> N; int step[N] = {0}; step[0] = 1; while(1){ if(now+count_bit(now) < N){ if(step[now+count_bit(now)-1] == 0){ step[now+count_bit(now)-1] = step[now-1]+1; a.push(now+count_bit(now)); } } else if(now+count_bit(now) == N){ cout << step[now-1]+1 << endl; return 0; } if(now-count_bit(now) > 0){ if(step[now-count_bit(now)-1] == 0){ step[now-count_bit(now)-1] = step[now-1]+1; a.push(now-count_bit(now)); } } if(a.empty()){ cout << -1 << endl; return 0; } now = a.front(); a.pop(); } } int count_bit(int x) { int bits = 0; while(1){ if(x%2 == 1) ++bits; x /= 2; if(x == 0) return(bits); } }