#include #include #include #include #include using namespace std; typedef deque Queue; static inline int popcount(unsigned int b) { #ifdef __GNUC__ return __builtin_popcount(b); #elif _MSC_VER >= 1400 return __popcnt(b); #else b = (b & 0x55555555) + (b >> 1 & 0x55555555); b = (b & 0x33333333) + (b >> 2 & 0x33333333); b = (b & 0x0F0F0F0F) + (b >> 4 & 0x0F0F0F0F); b += b >> 8; b += b >> 16; return b & 0x3F; #endif } int main(int argc, char *argv[]) { string s; getline(cin, s); int N = atoi(s.c_str()); int v[10001] = {}; Queue q; q.push_back(1); int ans = 0; while (q.size() > 0) { ++ans; Queue next; while (q.size() > 0) { int a = q.front(); int b = popcount(a); if (a == N) { cout << ans << endl; return 0; } v[a] = 1; q.pop_front(); if (a - b >= 1 && !v[a - b]) { next.push_back(a - b); } if (a + b <= N && !v[a + b]) { next.push_back(a + b); } } q = next; } ans = -1; cout << ans << endl; return 0; }