#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N; cin >> N; vector c(N+1,0); vector used(N+1, false); rep(i,N) c[i+1] = __builtin_popcount(i+1); queue> Q; Q.push({1,1}); while(!Q.empty()){ auto [now, cost] = Q.front(); Q.pop(); if(now == N){ cout << cost << endl; return 0; } int p = now + c[now]; if(p <= N && !used[p]){ Q.push({p,cost+1}); } int m = now - c[now]; if(m > 0 && !used[m]){ Q.push({m,cost+1}); used[m] = true; } } cout << -1 << endl; return 0; }