#include #include #include using namespace std; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int N; vectorseen; int main() { cin >> N; seen.resize(N+1); seen[1] = 1; queueq; q.push(1); while(!q.empty()) { int n =q.front(); q.pop(); int num = __builtin_popcount(n); int add_num = n + num; int sub_num = n - num; if (N >= add_num && seen[add_num] == 0) { seen[add_num] = seen[n] + 1; q.push(add_num); } if (sub_num > 0 && seen[sub_num] == 0) { seen[sub_num] = seen[n] + 1; q.push(sub_num); } } if (seen[N] == 0) { cout << -1 << endl; } else { cout << seen[N] << endl; } return 0; }