#include #include using namespace std; vector popcnt_cache; vector cnt_cache; int n; int popcnt(int n){ if(popcnt_cache[n]){ return popcnt_cache[n]; } int x=0; while(n){ x+=n & 1; n >>= 1; } popcnt_cache[n]=x; return x; } void calc(int i, unsigned int cnt){ if(cnt_cache[i]<=cnt) return; cnt_cache[i]=cnt; int p=popcnt(i); if(i+p<=n){ calc(i+p, cnt+1); } if(i-p>0){ calc(i-p, cnt+1); } } int main(){ cin >> n; popcnt_cache=vector(n+1, 0); cnt_cache=vector(n+1, -1); calc(1, 1); cout << (int)cnt_cache[n] << endl; return 0; }