#include #include using namespace std; #define MAX 10005 struct Node{ int x,cnt; }; int main() { int N; cin >> N; int ans = -1; queue q; bool visited[MAX] = {0}; Node tmp = {1,1}; q.push(tmp); visited[1] = true; while( q.size() ) { Node now = q.front(); q.pop(); int plus = __builtin_popcount(now.x); int rear = now.x + plus; int ex = now.x - plus; if(not visited[rear] and rear > 0) { Node node_r = {rear,now.cnt+1}; visited[rear] = true; q.push(node_r); } if( not visited[ex] && ex < N) { Node node_ex = {ex,now.cnt+1}; visited[ex] = true; q.push(node_ex); } if(rear == N) { ans = now.cnt+1; break; } } cout << ans << endl; return 0; }