#include using namespace std; using ll = long long; #define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i) #define all(a) (a).begin(), (a).end() int main() { cin.tie(nullptr); int N; cin >> N; vector dist(N, -1); queue que; dist[0] = 1; que.push(0); while (!que.empty()) { int v = que.front(); que.pop(); int d = __builtin_popcount(v + 1); if (v + d < N && dist[v + d] == -1) { dist[v + d] = dist[v] + 1; que.push(v + d); } if (v - d >= 0 && dist[v - d] == -1) { dist[v - d] = dist[v] + 1; que.push(v - d); } } cout << dist[N - 1] << '\n'; }