#include int count(int n) { int ret = 0; while (n) { if (n & 1) ret++; n >>= 1; } return ret; } int main() { int n; std::cin >> n; std::vector> g(n + 1); for (int i = 1; i <= n; i++) { int k = count(i); if (i - k > 0) g[i].push_back(i - k); if (i + k <= n) g[i].push_back(i + k); } std::vector d(n + 1, 100000000); d[1] = 1; std::deque q; q.push_back(1); while (!q.empty()) { auto curr = q.front(); for (auto to : g[curr]) { if (d[to] <= d[curr] + 1) continue; d[to] = std::min(d[to], d[curr] + 1); q.push_back(to); } q.pop_front(); } std::cout << (d[n] == 100000000 ? -1 : d[n]) << std::endl; return 0; }