#include #include #include #include #include using namespace std; int main() { int n, ans[10000], tmp; cin >> n; fill(ans, ans + n, 10000); queue< pair > q; q.push(pair(1, 1)); while (!q.empty()) { pair x = q.front(); q.pop(); if (ans[x.first - 1] <= x.second) continue; ans[x.first - 1] = x.second; bitset<32> s(x.first); tmp = s.count(); if (x.first + tmp <= n) q.push(pair(x.first + tmp, x.second + 1)); if (x.first > tmp) q.push(pair(x.first - tmp, x.second + 1)); } if (ans[n - 1] == 10000) cout << -1 << "\n"; else cout << ans[n - 1] << "\n"; return 0; }