#include"bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; #define rep(i,m,n) for(ll i=(ll)m;i<(ll)n;i++) #define Endl endl #define pr(i,j) make_pair(i,j) const ll mod = 998244353; const ll inf = 5e18; const ld pi = 3.14159265358979; ll popcount(ll n) { ll ret = 0; while (n > 0) { ret += n % 2; n /= 2; } return ret; } int main() { ll n; cin >> n; vectordist(n + 1, inf); dist[1] = 1; priority_queue, vector>, greater>>que; que.push(pr(0, 1)); while (!que.empty()) { ll now = que.top().second; que.pop(); ll b = now - popcount(now); ll f = now + popcount(now); if (1 <= b && b <= n) { if (dist[b] > dist[now] + 1) { que.push(pr(dist[now] + 1, b)); dist[b] = dist[now] + 1; } } if (1 <= f && f <= n) { if (dist[f] > dist[now] + 1) { que.push(pr(dist[now] + 1, f)); dist[f] = dist[now] + 1; } } } if (dist[n] == inf)cout << -1 << endl; else cout << dist[n] << endl; }