#include #include using namespace std; int count(int n) { if (n == 1) return 1; else if (n == 0) return 0; return n % 2 + count(n / 2); } int main() { int N; int visited[10010] = { 0 }; cin >> N; queue > qu; qu.push(make_pair(1,1)); while (1) { if (qu.empty()) { cout << -1 << endl; break; } int k = qu.front().first; int step = qu.front().second; if (k == N) { cout << step << endl; break; } qu.pop(); if (visited[k] == 1) continue; visited[k] = 1; if(k+count(k) <= N) qu.push(make_pair(k + count(k), step + 1)); if(k >= count(k)) qu.push(make_pair(k - count(k), step + 1)); } return 0; }