#include using namespace std; using ll = long long; using ld = long double; #define rep(i, r) for(int i = 0; i < (r); ++i) #define reps(i, s, r) for(int i = (s); i < (r); ++i) #define fore(i, m2) for(auto &i : m2) #define vi vector #define vl vector #define pl pair #define all(i) (i).begin(), (i).end() #define fs first #define sc second template bool chmin(T &i, T b) { if(i > b) { i = b; return true; } return false; } template bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } const ll INF = LONG_LONG_MAX / 3; const ll MOD = 1'000'000'007; const ll MAX = 2e5 + 5; int main() { ll n = 0, ans = INF; cin >> n; vl dp(n + 5), seen(n + 5); queue q; q.push({1, 1}); while(!q.empty()) { ll p, d; tie(p, d) = q.front(); q.pop(); if(p == n) { chmin(ans, d); } dp[p] = d; seen[p] = 1; ll bi = __popcount(p); if(p - bi > 0 && !seen[p - bi]) { seen[p - bi] = 1; q.push({p - bi, d + 1}); } if(p + bi <= n && !seen[p + bi]) { seen[p + bi] = 1; q.push({p + bi, d + 1}); } } if(ans == INF) ans = -1; cout << ans << endl; }