#include using namespace std; using Int = int_fast64_t; using Word = uint_fast64_t; using Int128 = __int128_t; using Word128 = __uint128_t; using VInt = vector; using VVI = vector; using VWord = vector; using VVW = vector; using VS = vector; using VVS = vector; using VB = vector; using VVB = vector; using PII = pair; using PWW = pair; using VPII = vector; using VPWW = vector; #define SZ(x) ((Int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define rep(i,n) for(Int i=0, i##_len=(n); i> n; queue que; bool used[10001]; que.push(PII(1,1)); while (!que.empty()) { PII p = que.front(); que.pop(); Int s = __builtin_popcount(p.first); if (p.first == n) { cout << p.second << '\n'; return 0; } if (p.first + s <= n && !used[p.first + s]) { que.push(PII(p.first + s, p.second + 1)); used[p.first + s] = true; } if (p.first - s >= 1 && !used[p.first - s]) { que.push(PII(p.first - s, p.second + 1)); used[p.first - s] = true; } } cout << "-1\n"; return 0; }