#include #include using namespace std; template inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} int sgrk[10051]; int dp[20001]; int main(){ int n; cin >> n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j*=2){ if((i & j)!=0) sgrk[i]++; } } memset(dp,1000000,sizeof(dp)); dp[1]=1; for(int i=1;i<=n;i++){ chmin(dp[i+sgrk[i]],dp[i]+1); if(i-sgrk[i]>0)chmin(dp[i-sgrk[i]],dp[i]+1); } cout << dp[n] << endl; }