#include #include using namespace std; const int MAX = 1e4 + 10; int N; int d[MAX]; // 10進法の整数を2進法で表したときの1の個数を数える int bit_digit(int n) { int res = 0; for (int i = 1; i < (1 << 16); i <<= 1) { res += ((n & i) ? 1 : 0); } return res; } // 幅優先探索 int bfs(int n) { queue q; q.push(n); int pos; d[1] = 1; while (!q.empty()) { pos = q.front(); q.pop(); if (pos == N) return d[pos]; int cnt = bit_digit(pos); if (pos - cnt >= 1) { if (!d[pos-cnt]) { d[pos-cnt] = d[pos] + 1; q.push(pos - cnt); } } if (pos + cnt <= N) { if (!d[pos+cnt]) { d[pos+cnt] = d[pos] + 1; q.push(pos + cnt); } } } return -1; } int main() { // 入力 cin >> N; // 幅優先探索 cout << bfs(1) << endl; return 0; }