#include using namespace std; int main(void) { int dp[10001] = {0, 1}; int bit[10001] = {0}; int n; cin >> n; for(int i = 0; i <= n; i++) { for(int j = 0; j <= 16; j++) if((i & (1 << j)) > 0) bit[i]++; } while(true) { bool find = false; for(int i = 1; i <= n; i++) { if(dp[i] == 0) continue; int f = i + bit[i]; int b = i - bit[i]; if(f >= 1) if(dp[i] + 1 < dp[f] || dp[f] == 0) { dp[f] = dp[i] + 1; find = true; } if(b <= n) if(dp[i] + 1 < dp[b] || dp[b] == 0) { dp[b] = dp[i] + 1; find = true; } } if(!find) break; } if(dp[n] != 0) cout << dp[n] << endl; else cout << -1 << endl; return 0; }