#include #include #include #include int bit_num(int num) { int count = 0; while (num != 0) { if (num % 2 == 1) count++; num /= 2; } return count; } int main() { int n; std::cin >> n; const int INF = 9999999; std::vector masu(n + 1); for (int i = 0; i < masu.size(); i++) masu[i] = INF; masu[1] = 1; std::queue s; s.push(1); while(!s.empty()) { int pos = s.front(); s.pop(); int bit = bit_num(pos); if (pos + bit < masu.size() && masu[pos + bit] > masu[pos] + 1) { masu[pos + bit] = masu[pos] + 1; s.push(pos + bit); } if (pos - bit > 0 && masu[pos - bit] > masu[pos] + 1){ masu[pos - bit] = masu[pos] + 1; s.push(pos - bit); } } if (masu[n] == INF) masu[n] = -1; std::cout << masu[n] << std::endl; return 0; }