#include using namespace std; typedef long long ll; #define REP(i,n) for(int i=0; i #define VLL vector #define VVI vector> #define VVLL vector> #define VC vector #define VS vector #define VVC vector> #define fore(i,a) for(auto &i:a) typedef pair P; template bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const int INF = 1 << 30; const ll INFL = 1LL<<60; const ll mod = 1000000007; int main() { vector used(10050, false); VI v(10050, 0); VI ans(10050, INF); int n; cin >> n; FOR(i, 1, 10001) { int j = i; while (j) { v[i] += j % 2; j /= 2; } } queue> q; q.push({ 1, 1 }); while (!q.empty()) { pair p = q.front(); used[p.first] = true; q.pop(); ans[p.first] = min(ans[p.first], p.second); if (p.first - v[p.first] > 0) { if (!used[p.first - v[p.first]]) { q.push({ p.first - v[p.first],p.second + 1 }); } } if (p.first + v[p.first] <= n) { if (!used[p.first + v[p.first]]) { q.push({ p.first + v[p.first],p.second + 1 }); } } } if (ans[n] == INF)cout << -1 << endl; else cout << ans[n] << endl; return 0; }