#include #include #include #include using namespace std; int INF = 1e9; int bfs(int n) { queue q; q.push(1); vector root(n + 1, INF); root[1]; while (q.size()) { int now = q.front(); q.pop(); int next = __builtin_popcount(now); if (now - next > 0 && root[next] == INF) { root[next] = now + 1; q.push(now - next); } if (now + next <= n && root[next] == INF) { root[next] == now + 1; q.push(now + next); } } if (root[n] == INF) return -1; else return root[n]; } int main() { int n; cin >> n; cout << bfs(n) << endl; }