#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main() { long long n; cin >> n; long long a = 1; long long cnt = 1; vector > > memo(32, vector >(1<<20, make_pair(-1, -1))); while(a < n){ int high = bitset<32>(a >> 20).count(); int low = a & ((1 << 20) - 1); if(memo[high][low].first != -1){ long long b = a + memo[high][low].first; if(b <= n){ a = b; cnt += memo[high][low].second; continue; } } long long b = a; int add = 0; while(b < n && (a >> 20) == (b >> 20)){ ++ add; b += high + bitset<32>(b & ((1 << 20) - 1)).count(); } if((a >> 20) != (b >> 20)) memo[high][low] = make_pair(b - a, add); a = b; cnt += add; } if(a == n) cout << cnt << endl; else cout << -1 << endl; return 0; }