#include using namespace std; #define rep(i, j, n) for(int i=j;i pi; template using vt = vector; template using vvt = vector>; i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));} i64 lcm(i64 n, i64 m) {return (n / gcd(n, m) * m);} int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; queue que; vt dist(n + 1, INF); que.push(1); dist[1] = 1; while(!que.empty()) { int p = que.front(); que.pop(); int cnt = 0; for(int bit = p; bit; bit /= 2) { if(bit & 1) cnt++; } if(p + cnt <= n && dist[p] + 1 < dist[p + cnt]) { dist[p + cnt] = dist[p] + 1; que.push(p + cnt); } if(p - cnt > 0 && dist[p] + 1 < dist[p - cnt]) { dist[p - cnt] = dist[p] + 1; que.push(p - cnt); } } if(dist[n] == INF) cout << -1 << endl; else cout << dist[n] << endl; }