#include #include #include using namespace std; const int INF = 1145141919; int bit_count(int x) { int ret = 0; while (x > 0) { if (x & 1)ret++; x >>= 1; } return ret; } int main() { int n; cin >> n; vector minCost(n + 1, INF); minCost[1] = 1; queue que; que.push(1); while (!que.empty()) { int here = que.front(); que.pop(); int move = bit_count(here); if (here + move <= n && minCost[here + move] == INF) { minCost[here + move] = minCost[here] + 1, que.push(here + move); } if (1 <= here - move && minCost[here - move] == INF) { minCost[here - move] = minCost[here] + 1, que.push(here - move); } } cout << (minCost[n] == INF ? -1 : minCost[n]) << endl; return 0; }