#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define all(x) (x).begin(),(x).end() #define rep(i,m,n) for(int i = m;i < n;++i) #define pb push_back #define fore(i,a) for(auto &i:a) #define rrep(i,m,n) for(int i = m;i >= n;--i) #define INF INT_MAX/2 using namespace std; using ll = long long; using R = double; using Data = pair>; const ll MOD = 1e9 + 7; const ll inf = 1LL << 50; struct edge { ll from; ll to; ll cost; }; typedef tupleT; typedef pairpint; int dist[10101]; int bitnum(int n){ int ret = 0; while (n > 0) { ret += n % 2; n /= 2; } return ret; } int main(){ int n; cin >> n; memset(dist, -1, sizeof(dist)); queueq; q.push(1); dist[1] = 1; while (q.size()) { int t = q.front(); q.pop(); int b = bitnum(t); int x1 = t + b; int x2 = t - b; if (1 <= x1 && x1 <= n && dist[x1] == -1) { dist[x1] = dist[t] + 1; q.push(x1); } if (1 <= x2 && x2 <= n && dist[x2] == -1) { dist[x2] = dist[t] + 1; q.push(x2); } } cout << dist[n] << endl; return 0; }