#include #include #include #include #include #include #include #include #include using namespace std; int now_bit(int num) { bitset<1000> bs(num); return bs.count(); } bool InBounds(int pos,int n) { if(pos > 0 && pos <= n) return true; else return false; } int main() { int n; cin >> n; if(n == 1) { cout << 1 << endl; return 0; } vector> used(n+1,vector(n+1)); queue> q; q.push(make_pair(1 + now_bit(1),1)); used.at(1).at(1+now_bit(1)) = 1; while(!q.empty()) { pair index = q.front(); q.pop(); if(index.first == n) { cout << index.second + 1 << endl; return 0; } if(InBounds(index.first + now_bit(index.first),n) && !used.at(index.first).at(index.first + now_bit(index.first))) { q.push(make_pair(index.first + now_bit(index.first),index.second + 1)); used.at(index.first).at(index.first + now_bit(index.first)) = 1; } if(InBounds(index.first - now_bit(index.first),n) && !used.at(index.first).at(index.first - now_bit(index.first))) { q.push(make_pair(index.first - now_bit(index.first),index.second + 1)); used.at(index.first).at(index.first - now_bit(index.first)) = 1; } } cout << -1 << endl; return 0; }