#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define getchar getchar_unlocked #define putchar putchar_unlocked using namespace std; typedef long long int64; typedef long long unsigned uint64; typedef long double float80; typedef unsigned short uint16; typedef unsigned uint; typedef unsigned char uint8; const uint pre = 18; const uint pre_total = 1u << pre; uint nsteps[41 - pre][pre_total]; uint nexts[41 - pre][pre_total]; uint pops[1u << 16]; inline uint pop_count(uint n) { return pops[n & 65535] + pops[n >> 16]; } void solve() { // @FF256grhy さんの解法 for (uint i = 0; i < 16; ++i) { uint beg = 1 << i; uint end = beg << 1; for (uint j = beg; j < end; ++j) { pops[j] = pops[j - beg] + 1; } } for (int i = 40 - pre; i >= 0; --i) { for (int j = pre_total - 1; j >= 0; --j) { uint k = j + i + pop_count(j); if (k >= pre_total) { nsteps[i][j] = 1; nexts[i][j] = k - pre_total; } else { nsteps[i][j] = nsteps[i][k] + 1; nexts[i][j] = nexts[i][k]; } } } uint64 n; while (~scanf("%llu", &n)) { uint hi = n >> pre; uint lo = n & (pre_total - 1); uint l = 1; uint64 nstep = 1; for (uint i = 0; i < hi; ++i) { uint pc = pop_count(i); nstep += nsteps[pc][l]; l = nexts[pc][l]; } uint pc_hi = pop_count(hi); while (l < lo) { l += pc_hi + pop_count(l); nstep += 1; } printf("%lld\n", l == lo ? nstep : -1); } } int main() { solve(); return 0; }