#include #include #include #include using namespace std; int bitcount(int bits) { int num = 0; for (; bits != 0; num++) bits &= (bits - 1); return num; } int main() { int N; cin >> N; vector visited(N + 1); queue > q; q.push(make_pair(1, 1)); int ans = -1; while (!q.empty()) { int i, cnt; tie(i, cnt) = q.front(); q.pop(); if (i == N) { ans = cnt; break; } visited[i] = true; int n = bitcount(i); if (i + n <= N && !visited[i + n]) q.push(make_pair(i + n, cnt + 1)); if (i - n >= 1 && !visited[i - n]) q.push(make_pair(i - n, cnt + 1)); } cout << ans << endl; return 0; }