#include #include #include #include #include #define REP(i,n) for(int i=0;i<(n);++i) #define SORT(a) sort((a).begin(),(a).end()) #define PB push_back using namespace std; typedef vector VI; typedef queue QI; int main(){ int N; cin >> N; int pc[10001], d[10001]; REP(i,10001){ int t = i, s = 0; while(t != 0){ if(t & 1){ ++s; } t >>= 1; } pc[i] = s; d[i] = -1; } QI q; q.push(1); d[1] = 0; while(d[N] && !q.empty()){ int c = q.front(); q.pop(); int cn = c - pc[c], cp = c + pc[c]; if(cp <= N && d[cp] == -1){ d[cp] = d[c] + 1; q.push(cp); } if(cn > 0 && d[cn] == -1){ d[cn] = d[c] + 1; q.push(cn); } } cout << d[N] << endl; }