#include using namespace std; #define MAX 1000000 int q[MAX][2]; int head = 0, tail = 0; void enq(int p, int kind) { q[tail][0] = p; q[tail][1] = kind; ++tail; if (tail >= MAX) { tail = 0; } } bool deq(int* p, int* kind) { if (head == tail) { return false; } else { *p = q[head][0]; *kind = q[head][1]; ++head; if (head >= MAX) { head = 0; } return true; } } int BinaryCount(int a) { /*int count = 0; while (n > 0) { count += n % 2; n /= 2; } return count;*/ int cnt = 0; while (a>0) { cnt++; a &= a - 1; } return cnt; } int main(void) { int n; cin >> n; bool* flag = new bool[n]; for (int i = 0; i < n; ++i) { flag[i] = false; } int posi = 1, hosuu; int kind = 1; do { if (posi == n) { cout << kind << endl; return 0; } flag[posi - 1] = true; hosuu = BinaryCount(posi); if (posi - hosuu > 0 && !flag[posi - 1 - hosuu]) { enq(posi - hosuu, kind + 1); } if (posi + hosuu <= n && !flag[posi - 1 + hosuu]) { enq(posi + hosuu, kind + 1); } } while (deq(&posi, &kind)); cout << "-1" << endl; return 0; }