#include using namespace std; int main() { cin.tie(0)->sync_with_stdio(0); int N; cin >> N; vector dp(N + 1, -1); dp[1] = 1; queue q; q.push(__builtin_popcount(1)); int dx[2] = { 1, -1 }; while (not q.empty()) { int cur = q.front(); int step = __builtin_popcount(q.front()); q.pop(); for (int k : dx) { int nxt = cur + step * k; if (nxt >= 1 and nxt <= N and dp[nxt] == -1) { q.push(nxt); dp[nxt] = dp[cur] + 1; } } } cout << dp[N] << endl; return 0; }