#include "bits/stdc++.h" using namespace std; #define REP(i,a) for(int i = 0; i < (a);i++) #define ALL(a) (a).begin(),(a).end() typedef long long ll; typedef pair P; const int INF = 1e9; const int MOD = 1e9 + 7; using Graph = vector < vector>; long int sum[200020] = {}; int main() { int N; cin >> N; queue que; vector dist(N+1,0); vector check(N+1,0); que.push(1); dist[1] = 1; check[1] = 1; while (!(que.empty())) { int a = que.front(); que.pop(); int sum = a; int count = 0; while (sum) { count = count + sum % 2; sum = sum / 2; } int b = a + count; int c = a - count; if (b <= N) { if (check[b] == 0) { que.push(b); dist[b] = 1 + dist[a]; check[b] = 1; if (b == N) { break; } } } if (c > 1) { if (check[c] == 0) { que.push(c); dist[c] = 1 + dist[a]; check[c] = 1; if (c == N) { break; } } } } if (check[N] == 0) { cout << -1; } else { cout << dist[N]; } }