#include #include #include #include #include #include #include #define REP(i, a, b) for (int i = int(a); i < int(b); i++) #ifdef _DEBUG_ #define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl #else #define dump(val) #endif using namespace std; typedef long long int ll; typedef pair P; template vector make_v(size_t a, T b) { return vector(a, b); } template auto make_v(size_t a, Ts... ts) { return vector(a, make_v(ts...)); } int popcount(int n) { int cnt = 0; while (n) { cnt += (n % 2); n /= 2; } return cnt; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; const int inf = 1 << 30; vector cost(n + 1, inf); cost[1] = 1; cost[0] = -1; queue

q; q.emplace(1, 1); int d[2] = {-1, 1}; while (q.size()) { int c, pos; tie(c, pos) = q.front(); q.pop(); if (cost[pos] < c) { continue; } int len = popcount(pos); REP(k, 0, 2) { int a = pos + d[k] * len; if (a <= 0 || a > n) continue; if (cost[a] > c + 1) { cost[a] = c + 1; q.emplace(cost[a], a); } } } if (cost[n] == inf) { cost[n] = -1; } cout << cost[n] << endl; return 0; }