#include #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define FORR(i,a,b) for (int i=(a);i>=(b);i--) #define pb push_back using namespace std; typedef long long ll; typedef pair pii; typedef vector vi; typedef set si; const int inf = 1e9; const int mod = 1e9+7; si y; queue q[2]; int n; main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n; int count = 1; q[1].push(1); y.insert(1); while(true){ int b = count % 2; if(q[b].empty())break; while(!q[b].empty()){ int tmp = q[b].front(); q[b].pop(); if(tmp == n){ cout << count << endl; return 0; } int bc = 0, _tmp = tmp; while(_tmp > 0){ if(_tmp & 1)bc++; _tmp >>= 1; } if(tmp - bc > 0 && y.find(tmp - bc) == y.end()){ q[1-b].push(tmp - bc); y.insert(tmp - bc); } if(tmp + bc <= n && y.find(tmp + bc) == y.end()){ q[1-b].push(tmp + bc); y.insert(tmp + bc); } } count++; } cout << -1 << endl; return 0; }