#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #ifdef __unix__ #include #else #include "bits\stdc++.h" #endif #include #include #include #include #include #define REP(i,a,b) for(i=a;i #define ll long long #define ull unsigned ll typedef long double ld; #define SQR(X) ((X)*(X)) #define MAX 10005 int pop_cnt(int x) { int cnt = 0; while(x) { if(x % 2) cnt++; x /= 2; } return cnt; } struct Node{ int x,cnt; }; int main() { int N; cin >> N; if(N == 1) cout << 1 << endl; 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 = pop_cnt(now.x); int rear = now.x + plus; int ex = now.x - plus; if(rear == N) { ans = now.cnt+1; } if(not visited[rear] and rear < N) { Node node_r = {rear,now.cnt+1}; visited[rear] = true; q.push(node_r); } if( not visited[ex] && ex > 0) { Node node_ex = {ex,now.cnt+1}; visited[ex] = true; q.push(node_ex); } } cout << ans << endl; return 0; }