#include #include int num_of_1(const int &n) { if (n == 0) { return 0; }else{ return num_of_1(n >> 1) + (n & 1); } } class Cell { public: Cell() :vector(0),dist(100000) {}; void add_prev(Cell* other) { vector.push_back(other); } void solve(const int &count = 1) { if (dist > count) { dist = count; for (auto &cell : vector) { cell->solve(count + 1); } } } int get_dist() { return dist; } private: std::vector vector; int dist; }; int main() { int n; std::cin >> n; std::vector < Cell> vector(n); for (auto i = 0; i < n; ++i) { auto step = num_of_1(i + 1); if (i - step > 0) { vector.at(i - step).add_prev(&vector.at(i)); } if (i + step < n) { vector.at(i + step).add_prev(&vector.at(i)); } } vector.at(n - 1).solve(); if (vector.at(0).get_dist() > n) { std::cout << -1 << std::endl; } else { std::cout << vector.at(0).get_dist() << std::endl; } return 0; }