#include #include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template constexpr T INF = ::numeric_limits::max()/32*15+208; int main() { int n; cin >> n; vector d(n, INF); vector> v(n); for (int i = 0; i < n; ++i) { int k = __builtin_popcount(i+1); if(i > k) { v[i].emplace_back(i-k); } if(i+k < n){ v[i].emplace_back(i+k); } } stack> Q; Q.emplace(0, 1); while(!Q.empty()){ auto [x, c] = Q.top(); Q.pop(); d[x] = c; for (auto &&i : v[x]) { if(c+1 < d[i]){ Q.emplace(i, c+1); } } } if(d[n-1] == INF) puts("-1"); else cout << d[n-1] << "\n"; return 0; }