#include #include #include #include #include #include #include #define MAX 10000 using namespace std; typedef long long int lint; typedef pair Pii; typedef pair PII; bool visited[MAX + 1]; int bit(int N) { int ret = 0; for (int i = 1; i <= 10000; i <<= 1) { ret += ((N & i) ? 1 : 0); } return ret; } int main() { int N, pos, step, jump; cin >> N; stack st; st.push(Pii(1, 1)); while (!st.empty()) { pos = st.top().first; step = st.top().second; if (pos == N) { cout << step << endl; return 0; } st.pop(); visited[pos] = true; jump = bit(pos); if (pos + jump <= N) if(!visited[pos + jump]) st.push(Pii(pos + jump, step + 1)); if (pos - jump > 0) if (!visited[pos - jump]) st.push(Pii(pos - jump, step + 1)); } cout << -1 << endl; return 0; }