#include #include #include #include #include #include #include #include using namespace std; typedef long long int lint; int bit(int N) { int ret = 0; for (int i = 1; i <= (1 << 16); i <<= 1) { ret += ((N & i) ? 1 : 0); } return ret; } int game(int N) { int pos, step, jump; vector visit(N + 1, 0); queue qu; qu.push(1); visit[1] = 1; while (!qu.empty()) { pos = qu.front(); step = visit[pos]; qu.pop(); if (pos == N) { return step; } jump = bit(pos); if (pos + jump <= N) { if (visit[pos + jump] == 0) { qu.push(pos + jump); visit[pos + jump] = step + 1; } } if (pos - jump > 0) { if (visit[pos - jump] == 0) { qu.push(pos - jump); visit[pos - jump] = step + 1; } } } return -1; } int main() { int N; cin >> N; cout << game(N) << endl; return 0; }