#include #include #include using namespace std; const int INF = 1e+9; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int ans = -1; queue que; que.push(1); bool visited[10001] = {0}; int d[10001]; for(int i = 0; i <= n; ++i) d[i] = INF; d[1] = 1; while(!que.empty()){ int p = que.front(); que.pop(); if(n == p){ ans = d[p]; break; } if(visited[p]){ continue; } visited[p] = true; int next = __builtin_popcount(p); if(next + p <= n){ d[next + p] = min(d[next + p], d[p] + 1); que.push(next + p); } if(p - next >= 1){ d[p - next] = min(d[p - next], d[p] + 1); que.push(p - next); } } cout << ans << endl; return 0; }