#include #include #include using namespace std; int count_one(int n){ int count = 0; while(n > 0){ if(n % 2 == 1) count++; n /= 2; } return count; } int main(){ int N; cin >> N; int count = 1; vector pathed(N,0); queue> q; q.push(make_pair(1,1)); while(!q.empty()){ int now = q.front().first; if(now == N) { cout << q.front().second; return 0; } int next_go = now + count_one(now); int next_back = now - count_one(now); //cout << "now:" << now << ",count:" << q.front().second << endl; count = q.front().second; count++; if(next_go == N){ cout << count << endl; return 0; } q.pop(); if(next_go < N){ if(pathed.at(next_go) != 1){ q.push(make_pair(next_go, count)); pathed.at(next_go) = 1; } } if(pathed.at(next_back) != 1){ q.push(make_pair(next_back, count)); pathed.at(next_back) = 1; } } cout << -1 << endl; }