#include #include #include using namespace std; using uint = unsigned int; int bitCount(uint bits) { int count = 0; for (uint mask = 1; mask <= bits; mask<<=1) { if ((bits & mask) != 0) ++count; } return count; } int main() { uint n; cin >> n; queue positions; positions.push(1); vector move_n(n + 1, 0); move_n[1] = 1; while (!positions.empty()) { uint pos = positions.front(); positions.pop(); if (pos == n) { cout << move_n[pos] << endl; return 0; } int bit_cnt = bitCount(pos); int new_pos0 = pos + bit_cnt; int new_pos1 = pos - bit_cnt; if (new_pos0 <= n && move_n[new_pos0] == 0) { positions.push(new_pos0); move_n[new_pos0] = move_n[pos] + 1; } if (new_pos1 > 0 && move_n[new_pos1] == 0) { positions.push(new_pos1); move_n[new_pos1] = move_n[pos] + 1; } } cout << -1 << endl; }