#include using i64 = long long; int main() { std::cin.tie(nullptr)->sync_with_stdio(false); int n; std::cin >> n; constexpr i64 inf = std::numeric_limits::max(); std::vector dp(n + 1, inf); dp[0] = 1; std::queue q; q.push(0); while (q.size()) { int d = q.front(); q.pop(); auto e = __builtin_popcount(d + 1); if (e + d < n && dp[e + d] == inf) { dp[d + e] = std::min(dp[e + d], dp[d] + 1); q.push(e + d); } if (d - e >= 0 && dp[d - e] == inf) { dp[d - e] = std::min(dp[d - e], dp[d] + 1); q.push(d - e); } } std::cout << (dp[n - 1] == inf ? -1 : dp[n - 1]) << '\n'; return 0; }