#include #include #define ll long long using namespace std; typedef pair P; int bitcnt(int n){ int bit = 0; while(n > 0){ if(n & 1){ bit++; } n >>= 1; } return bit; } int main(){ int n; cin >> n; queue

que; que.push(P(1,1)); vector used(n+1,false); while(!que.empty()){ int now = que.front().first; int cost = que.front().second; que.pop(); used[now] = true; if(now == n){ cout << cost << endl; return 0; } int next = now + bitcnt(now); int prev = now - bitcnt(now); if(next > 0 && next <= n && !used[next]){ que.push(P(next,cost+1)); } if(prev > 0 && prev <= n && !used[prev]){ que.push(P(prev,cost+1)); } } cout << -1 << endl; return 0; }