#include #include #include #include struct Step { int curr_n; int cost; }; auto count_one(int n) -> int { int count = 0; for (int b = 1; b <= 10000; b <<= 1) { if ((n & b) != 0) { count++; } } return count; } auto main() -> int { int last_n; std::cin >> last_n; std::queue steps; std::vector costs(last_n, std::numeric_limits::max()); steps.push({1, 1}); while (!steps.empty()) { Step s = steps.front(); steps.pop(); if (s.cost <= costs[s.curr_n - 1]) { costs[s.curr_n - 1] = s.cost; int n_one = count_one(s.curr_n); if ((s.curr_n + n_one) <= last_n) { steps.push({s.curr_n + n_one, s.cost + 1}); } if ((s.curr_n - n_one) >= 1) { steps.push({s.curr_n - n_one, s.cost + 1}); } } } int smallestCost = costs[last_n - 1]; if (smallestCost == std::numeric_limits::max()) { std::cout << -1 << std::endl; } else { std::cout << smallestCost << std::endl; } }