#include #include #include #define print(x) cout << x << endl; using namespace std; queueQ;//キュー bool visited[10001]; //訪問済みチェック int counter[10001]; //あるマス目までの最短移動数 /*ビットカウント*/ int BitNum(int bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff); } int main(void){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int i, mov, next, c; visited[1] = true; counter[1] = 1; counter[N] = -1; Q.push(1); /*幅優先探索*/ while(!Q.empty()){ i = Q.front(); Q.pop(); c = counter[i]; mov = BitNum(i); if((i+mov) == N){ next = i + mov; visited[next] = true; counter[next] = c + 1; break; } if(!visited[i+mov] && (i+mov) < N){ next = i + mov; Q.push(next); visited[next] = true; counter[next] = c + 1; } if(!visited[i-mov] && (i-mov) > 1){ next = i - mov; Q.push(next); visited[next] = true; counter[next] = c + 1; } } print(counter[N]); return 0; }