#include int N; int bits(int position) { int b = 0; while (position > 0) { if ((position&1) > 0) b++; position = position >> 1; } return b; } int move(int position, int step) { int b = bits(position); int fpos = -1, bpos = -1; if (position+b <= N) fpos = move(position+b, step+1); if (position-b > 0) bpos = move(position-b, step+1); if (fpos == -1) { return bpos; } else { if (bpos == -1) return fpos; else if (fpos < bpos) return fpos; else return bpos; } } main() { scanf("%d", &N); int result = move(1, 0); printf("%d\n", result); }