#include #include using namespace std; const int INF = 1000000; int N; int d[10001]; int main() { cin >> N; for (int i = 0; i <= 10000; i++) { d[i] = INF; } queue que; d[1] = 1; que.push(1); while (!que.empty()) { int n = que.front(); que.pop(); int bn = __builtin_popcount(n); if (n - bn >= 1 && d[n - bn] == INF) { d[n - bn] = d[n] + 1; que.push(n - bn); } if (n + bn <= N) { d[n + bn] = d[n] + 1; que.push(n + bn); } } cout << (d[N] != INF ? d[N] : -1) << endl; return 0; }