#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] = 1; while (q.size()) { int now = q.front(); q.pop(); int next = __builtin_popcount(now); if (now - next > 0 && root[now - next] == INF) { root[next - now] = now + 1; q.emplace(now - next); } if (now + next <= n && root[now + next] == INF) { root[next + now] == now + 1; q.emplace(now + next); } } if (root[n] == INF) return -1; else return root[n]; } int main() { int n; cin >> n; cout << bfs(n) << endl; }