#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define minimize(a, x) a = std::min(a, x) #define maximize(a, x) a = std::max(a, x) using ll = long long; int const inf = 1<<29; int main() { int N; cin >> N; queue> q; q.emplace(1, 0); set st; st.emplace(1); while(!q.empty()) { int p, cc; tie(p, cc) = q.front(); q.pop(); if(p == N) { cout << cc+1 << endl; exit(0); } int c = __builtin_popcount(p); if(p+c <= N && !st.count(p+c)) q.emplace(p+c, cc+1), st.insert(p+c); if(p-c >= 1 && !st.count(p-c)) q.emplace(p-c, cc+1), st.insert(p-c); } cout << -1 << endl; return 0; }