#include "bits/stdc++.h" using namespace std; #define rt return #define FOR(i,j,k) for(int i=j; i<(int)k;++i) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define mt make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef int Weight; struct Edge{ int from, to; Weight wei; Edge(int from_, int to_, Weight wei_):from(from_),to(to_),wei(wei_){} }; typedef vector Edges; typedef vector Graph; bool operator>(const Edge &a, const Edge &b){ return a.wei>b.wei; } vector bfs(Graph &G, int start){ vector res(G.size(), -1); queue > Q; Q.push(make_pair(start, 0)); while(Q.size()){ auto p = Q.front(); Q.pop(); if(res[p.first] != -1) continue; res[p.first] = p.second; for(auto &e : G[p.first]) Q.push(make_pair(e.to, p.second+1)); } return res; } int bitcnt(int n){ int res = 0; while(n){ if(n&1) ++res; n>>=1; } return res; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; N++; Graph g(N); g[0].emplace_back(0, 1, 1); for(int i = 1; i < N; ++i){ int x = bitcnt(i); if(i - x >= 1)g[i].emplace_back(i, i - x, 1); if(i + x < N)g[i].emplace_back(i, i + x, 1); } cout << bfs(g, 0).back() << endl; }