#include using namespace std; bool done[10001]; int memo[10001]; int N; int bitcount(int a) { int cnt = 0; while (a) { cnt += a & 1; a >>= 1; } return cnt; } int main() { cin >> N; memset(memo, -1, sizeof(memo)); queue q; q.push(1); memo[1] = 1; while (q.size()) { int a = q.front(); q.pop(); if (done[a]) continue; done[a] = true; int b = bitcount(a); if (a + b <= N) { q.push(a + b); memo[a + b] = memo[a] + 1; } if (a - b >= 1) { q.push(a - b); memo[a - b] = memo[a] + 1; } } cout << memo[N] << endl; }