#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define INF (INT_MAX/3) #define PI (2*acos(0.0)) #define EPS (1e-8) typedef long long ll; typedef unsigned long long ull; int N; int cntBit(int b){ int cnt = 0; for(int i = 0; i < 14; i++) cnt += (b & (i << 1)); return cnt; } int solve(){ int minDis = INF; pair s(1, 1); queue > que; set e; que.push(s); while(!que.empty()){ pair n = que.front(); que.pop(); if(n.first > N || n.first < 1 || e.find(n.first) != e.end()) continue; if(n.first == N) { minDis = min(minDis, n.second); break;} e.insert(n.first); int b = __builtin_popcount(n.first); que.push(pair(n.first + b, n.second + 1)); if(n.first - b != 1) que.push(pair(n.first - b, n.second + 1)); } return minDis; } int main(){ ios_base::sync_with_stdio(0); cin >> N; int minDis = solve(); if(minDis == INF) cout << -1 << endl; else cout << minDis << endl; return 0; }