#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, m, n) for(int i = (m); i < (int)(n); ++i) #define ALL(x) (x).begin(), (x).end() #define INF 2000000000 #ifdef LOCAL #define eprintf(...) fprintf(stdout, __VA_ARGS__) #else #define eprintf(...) 0 #endif using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef unsigned int uint; int popcount(int n) { bitset b(n); return b.count(); } int main() { int n; cin >> n; vector visited(n+1, false); deque q; q.push_back(1); bool cangoal = false; int count = 0; while(!q.empty()) { int cur = q.front(); q.pop_front(); int pc = popcount(cur); eprintf("cur:%d pc:%d\n", cur, pc); if(cur + pc == n) { eprintf("found! cur:%d goto:%d\n", cur, cur-pc); cangoal = true; break; } int back = cur - pc; int forward = cur + pc; printf("back:%d forward:%d n:%d\n", back, forward, n); if(back >= 1 && !visited[back]) { eprintf("cur:%d goto:%d\n", cur, back); q.push_back(back); visited[back] = true; } if(forward <= n && !visited[forward]) { eprintf("cur:%d goto:%d\n", cur, forward); q.push_back(forward); visited[forward] = true; } count++; } if(cangoal) { cout << count << endl; } else { cout << -1 << endl; } return 0; }