#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using vb = vector; using vvb = vector; using vd = vector; using vs = vector; using pii = pair; using pll = pair; using pdd = pair; using vpii = vector; using vpll = vector; using vpdd = vector; const int inf = (1 << 30) - 1; const ll INF = 1LL << 60; const int MOD = 1000000007; //const int MOD = 998244353; int main() { int n; cin >> n; vi dist(n + 1, inf); dist[1] = 1; queue que; que.push(1); while (!que.empty()) { int v = que.front(); que.pop(); int d = __builtin_popcount(v); if (v + d <= n) { if (dist[v + d] > dist[v] + 1) { dist[v + d] = dist[v] + 1; que.push(v + d); } } if (v - d >= 1) { if (dist[v - d] <= dist[v] + 1) continue; dist[v - d] = dist[v] + 1; que.push(v - d); } } if (dist[n] == inf) cout << -1 << endl; else cout << dist[n] << endl; return 0; }