#include #include #include #define UKN -1 using namespace std; int main() { int n; scanf("%d", &n); vector squares(n, 0); for (int i = 0; i < n; i++) { unsigned int v = i + 1; unsigned int c; for (c = 0; v; v >>= 1) { c += v & 1; } squares[i] = c; } vector least_steps(n, UKN); least_steps[0] = 1; queue reachable_sqs; reachable_sqs.push(0); int goal_idx = n - 1; while (!reachable_sqs.empty()) { int cur_idx = reachable_sqs.front(); reachable_sqs.pop(); int propulsion = squares[cur_idx]; if (cur_idx + propulsion <= goal_idx && least_steps[cur_idx + propulsion] == UKN) { least_steps[cur_idx + propulsion] = least_steps[cur_idx] + 1; reachable_sqs.push(cur_idx + propulsion); } if (cur_idx - propulsion >= 0 && least_steps[cur_idx - propulsion] == UKN) { least_steps[cur_idx - propulsion] = least_steps[cur_idx] + 1; reachable_sqs.push(cur_idx - propulsion); } } printf("%d", least_steps[goal_idx]); }