#include #include #include using namespace std; int main(){ bitset<10001> flag; int n; cin >> n; int* table; table = new int[n + 1]; table[0] = 0; for (int i = 0; i < n; i++){ flag.set(i, true); bitset<14> b(i + 1); int t = 0; for (int j = 0; j < 14; j++){ if (b[j]) t++; } table[i + 1] = t; } vector now; vector next; now.push_back(1); if (n == 1){ cout << "1" << endl; goto EXIT; } flag[1] = false; for (int i = 0; i < n; i++){ int len = now.size(); for (int j = 0; j < len; j++){ int tmp1 = now[j] + table[now[j]]; int tmp2 = now[j] - table[now[j]]; if (tmp1 == n || tmp2 == n){ cout << i + 2 << endl; goto EXIT; } if (tmp1 < n&&flag[tmp1]){ flag[tmp1] = false; next.push_back(tmp1); } if (tmp2>0 && flag[tmp2]){ flag[tmp2] = false; next.push_back(tmp2); } } if (next.empty()) goto NOT_FOUND; now = next; next.clear(); } NOT_FOUND: cout << "-1" << endl; EXIT: delete[] table; return 0; }