#include #include using namespace std; int fnNumOneInBit(int n){ int result=0; while(n>0){ if(n%2==1) ++result; n/=2; } return result; } int main(){ int tmp; int now, d; queue Queue; cin >> tmp; //入力 const int n=tmp; int depth[n]={0}; bool bLoop=true; int depthMin; //幅優先探索 Queue.push(1); depth[0]=1; while(!Queue.empty()){ now=Queue.front(); Queue.pop(); if(now==n){ bLoop=false; break; }else{ d=fnNumOneInBit(now); for(int i=0; i<2; ++i){ tmp = (i==0) ? (now-d) : (now+d); if(tmp>0 && tmp<=n && depth[tmp-1]==0){ Queue.push(tmp); depth[tmp-1]=depth[now-1]+1; } } } } if(bLoop) depthMin=-1; else depthMin=depth[n-1]; cout << depthMin << endl; //出力 return 0; }