#define _CRT_SECURE_NO_WARNINGS // #define _GLIBCXX_DEBUG #include using namespace std; typedef long long ll; // #define int ll typedef vector vi; typedef vector vvi; typedef pair pii; #define all(c) begin(c), end(c) #define range(i,a,b) for(ll i=a; i ostream & operator << (ostream &os, vector const &); template typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple const &){} template typename enable_if<(n< sizeof...(T))>::type _ot(ostream &os, tuple const &t){ os << (n==0?"":" ") << get(t); _ot(os, t); } template ostream & operator << (ostream &os, tuple const &t){ _ot<0>(os, t); return os; } template ostream & operator<<(ostream &os, pair const &p){ return os << "(" << p.first << ", " << p.second << ") "; } template ostream & operator<<(ostream &os, vector const &v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; } #ifdef DEBUG #define dump(...) (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) \ << " [" << __LINE__ << "]" << endl) #else #define dump(...) #endif void fastios(){ ios_base::sync_with_stdio(0); cin.tie(0); // #define endl '\n' } template size_t uniq(vector &v){ sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v.size(); } template size_t uniq(T *l, size_t n){ sort(l,l+n); return unique(l,l+n) - l; } #define mems(arr,val) memset(arr,val,sizeof(arr)); int const mod = 1000000007; int const inf = numeric_limits::max()/8; template inline size_t popcount(T x){ static_assert(is_integral::value, "popcount : must be integer."); static_assert(sizeof(x) == sizeof(int8_t) || sizeof(x) == sizeof(int16_t) || sizeof(x) == sizeof(int32_t) || sizeof(x) == sizeof(int64_t) || sizeof(x) == sizeof(__int128_t), "popcount : must be 8, 16, 32, 64, or 128 bits."); static const int M = 65535; static size_t dp[66000]; if(dp[M] != 16) for(int i = 0; i <= M; i++) dp[i] = (i&1) + dp[i>>1]; switch(sizeof(T)){ case sizeof(int8_t): return dp[x]; case sizeof(int16_t): return dp[(int8_t)x]; case sizeof(int32_t): return dp[x&M] + dp[x>>16&M]; case sizeof(int64_t): return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M]; case sizeof(__int128_t): return dp[x&M] + dp[x>>16&M] + dp[x>>32&M] + dp[x>>48&M] + dp[x>>64&M] + dp[x>>80&M] + dp[x>>96&M] + dp[x>>112&M]; } } template int solve(T n){ queue q; q.push(1); int dist[11111]; fill_n(dist,11111,1e9); dist[1] = 0; while(q.size()){ T c = q.front(); q.pop(); if(c == n) return dist[c]; int d = popcount(c); for(int w : {1,-1}){ T nx = c + d*w; if(1 <= nx && nx <= n && dist[nx] > dist[c] + 1){ dist[nx] = dist[c] + 1; q.push(nx); } } } return -1; } int main(){ int N; while(cin >> N){ vector ans; ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); ans.pb(solve(N)); dump(ans); // assert(all_of(ans.begin(),ans.end(),[&](int a){ // return a == ans[0]; // })); cout << ans[0] << endl; } }