#include #include using namespace std; const int INF = 1 << 29; int dp[10010]; int solve(int N){ for(int i=1;i<=N;i++)dp[i] = INF; queue Q; dp[1] = 0; Q.push(1); while(!Q.empty()){ int now = Q.front(); Q.pop(); int cost = dp[now]; int c = __builtin_popcount(now); if(now - c >= 1 && cost + 1 < dp[now - c]){ dp[now - c] = cost + 1; Q.push(now - c); } if(now + c <= N && cost + 1 < dp[now + c]){ dp[now + c] = cost + 1; Q.push(now + c); } } if(dp[N] == INF)return -1; return dp[N] + 1; } int main(){ int N; cin >> N; cout << solve(N) << endl; return 0; }