#include #define REP(i, b) for(int i = 0; i < (b); i++) #define REPS(i, b) for(int i = 1; i <= (b); i++) #define ALL(v) (v).begin(), (v).end() using namespace std; using ll = long long; using ld = long double; using pi = pair; using pl = pair; using vi = vector; using vl = vector; using vd = vector; using vs = vector; using vb = vector; using vpi = vector; using vpl = vector; using vvi = vector; using vvl = vector; using vvb = vector; const int INF = 1e9; const int MOD = 1e9+7; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } signed main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); int N; cin >> N; vi d(N+1, INF); d[1] = 0; queue q; q.push(1); while(!q.empty()) { int p = q.front(); q.pop(); int b = __builtin_popcount(p); if(p+b <= N && chmin(d[p+b], d[p]+1)) q.push(p+b); if(p-b >= 1 && chmin(d[p-b], d[p]+1)) q.push(p-b); } if(d[N] == INF) cout << -1 << endl; else cout << d[N]+1 << endl; }