#include using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--) #define pb push_back #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() #define fi first #define se second typedef long long ll; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll MOD = 1e9 + 7; double EPS = 1e-8; const int MAX_N = 10010; vector es[MAX_N]; ll d[MAX_N]; int main(){ int n; cin >> n; auto add = [&](int frm, int to) { if(to >= 1 && to <= n) es[frm].push_back(to); }; REP(i, 1, n+1) { int k = __builtin_popcount(i); add(i, i - k); add(i, i + k); } rep(i, MAX_N) d[i] = 1e18; int root = 1; auto dijkstra = [&] () { typedef pair P; //dist, from priority_queue, greater

> q; d[root] = 0LL; q.push({0LL, root}); while (!q.empty()) { auto p = q.top(); q.pop(); int prv = p.se; ll cost = p.fi; if (d[prv] < p.fi) continue; for (auto& to : es[prv]) { ll dist = 1 + cost; if (dist < d[to]) { d[to] = dist; q.push({dist, to}); } } } }; dijkstra(); cout << (d[n] == 1e18 ? -1 : d[n]+1) << endl; // REP(i, 1, n+1) cout << i << " : " << d[i] << endl; return 0; }