#include #include #include #include #include #include #include using namespace std; typedef long long LL; int main(){ int N; cin >> N; vector prev, next; prev.push_back(0); vector arrived(N); int cnt = 0; int ans = -1; if(N == 1) ans = 0; while(!prev.empty() && N >= 2){ cnt++; for(int i = 0; i < prev.size(); i++){ int pos = prev[i]; if(arrived[pos]) continue; arrived[pos] = 1; if(pos == N-1){ ans = cnt; break; } int n = pos+1; int bit_cnt = 0; while(n > 0){ bit_cnt += n%2; n /= 2; } if(pos-bit_cnt >= 0){ next.push_back(pos-bit_cnt); } if(pos+bit_cnt < N){ next.push_back(pos+bit_cnt); } } prev = next; next.clear(); } cout << ans << endl; return 0; }