#include #include #include using namespace std; int countstep(int n) { int cnt = 0; while(n != 0){ if(n % 2 == 1) cnt++; n /= 2; } return cnt; } int main() { int n; cin >> n; vector step(n+1,0); for(int i=1; i<=n; i++) step[i] = countstep(i); vector route(n+1,-1); route[1] = 1; queue pos; pos.push(1); while(!pos.empty()){ int p = pos.front(); pos.pop(); int pf = p + step[p]; if(pf <= n && (route[pf] == -1 || route[pf] > route[p] + 1)){ route[pf] = route[p] + 1; pos.push(pf); } int pb = p - step[p]; if(pb > 0 && (route[pb] == -1 || route[pb] > route[p] + 1)){ route[pb] = route[p] + 1; pos.push(pb); } } cout << route[n] << endl; return 0; }