#include #include #include #define REP(a, b) for(int a=0; a<(int)b; a++) using namespace std; int bit(int num){ int cnt=0; while(num!=0){ if((num&1) == 1) cnt++; num /= 2; } return cnt; } int main(){ int N; int min_num = -1; bool flag = false; int cnt = 0; cin >> N; vector sugoroku; sugoroku.resize(N+1); sugoroku[1] = 1; while(true){ flag = false; REP(i, N-1){ int pos = i+1; if(sugoroku[pos]==0) continue; int nextpos = bit(pos)+pos; if(nextpos <= N){ int nowcnt = sugoroku[nextpos]; sugoroku[nextpos] = (sugoroku[nextpos]==0) ? sugoroku[pos]+1 : min(sugoroku[pos]+1, sugoroku[nextpos]); if(nowcnt != sugoroku[nextpos]) flag = true; } nextpos = pos - bit(pos); if(nextpos >= 1){ int nowcnt = sugoroku[nextpos]; sugoroku[nextpos] = (sugoroku[nextpos]==0) ? sugoroku[pos]+1 : min(sugoroku[pos]+1, sugoroku[nextpos]); if(nowcnt != sugoroku[nextpos]) flag = true; } } // REP(i, N) cout << sugoroku[i+1] << ", "; //cout << endl; if(!flag) break; if(sugoroku[N]!=0) min_num = (min_num==-1)? sugoroku[N] : min(min_num,sugoroku[N]); } cout << min_num << endl; return 0; }