#include using namespace std; typedef int64_t ll; ll bitCount(ll n){ ll res=0; while(n>0){ res+=n&1; n>>=1; } return res; } void solve() { cin.tie(nullptr); ios::sync_with_stdio(false); ll n; cin >> n; unordered_set seen; deque> dq; dq.push_back({1,1}); while(!dq.empty()){ auto [a,b]=dq.front(); dq.pop_front(); if(a==n){ cout << b << endl; return; } if(seen.count(a)) continue; seen.insert(a); if(a-bitCount(a)>=1){ dq.push_back({a-bitCount(a),b+1}); } if(a+bitCount(a)<=n){ dq.push_back({a+bitCount(a),b+1}); } } cout << -1 << endl; } int main() { solve();system("pause"); }