#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define INF 1000000000 using namespace std; typedef long long ll; const int MAXN = 10010; int N; int dist[MAXN]; int getBitNum(int n) { int ret = 0; for (int i = 0; i < 30; i++) { if ((n >> i) & 1) ret++; } return ret; } int main(void) { cin >> N; for (int i = 0; i <= N; i++) dist[i] = INF; queue que; que.push(1); dist[1] = 1; while (!que.empty()) { int now = que.front(); que.pop(); int bit = getBitNum(now); if (now - bit > 0 && dist[now-bit] >= INF) { dist[now-bit] = dist[now] + 1; que.push(now-bit); } if (now + bit > 0 && dist[now+bit] >= INF) { dist[now+bit] = dist[now] + 1; que.push(now+bit); } } if (dist[N] < INF) cout << dist[N] << endl; else cout << -1 << endl; return 0; }