#include //cin, cout #include //vector #include //sort,min,max,count #include //string,getline, to_string #include //abs(int) #include //swap, pair #include //deque #include //INT_MAX #include //bitset using namespace std; int main() { int N; cin >> N; vector min_cost(N + 1, INT_MAX); deque next_posi; deque next_cost; next_posi.push_back(1); next_cost.push_back(1); while (!next_posi.empty()) { //現在値情報の引き出し int now_posi = next_posi.front(); int now_cost = next_cost.front(); next_posi.pop_front(); next_cost.pop_front(); //終了2:過去ベストとの比較 if (now_cost >= min_cost[now_posi]) { continue; } else { min_cost[now_posi] = now_cost; } //終了3:ゴール到達 if (now_posi == N) { continue; } //移動量 bitset<16> bs(now_posi); int step = bs.count(); //前:次週位置の追加 if (now_posi - step >= 1) { next_posi.push_back(now_posi - step); next_cost.push_back(now_cost + 1); } //後:次週位置の追加 if (now_posi + step <= N) { next_posi.push_back(now_posi + step); next_cost.push_back(now_cost + 1); } } if (min_cost[N] == INT_MAX) { cout << -1 << endl; } else { cout << min_cost[N] << endl; } return 0; }