#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; bool flag[10010]; int num[10010]; static const int INF=1e8; int cnt(int now){ int ans=0; while(now>0){ if(now%2==1){ ans++; } now/=2; } return ans; } int main(){ memset(flag, 0, sizeof(flag)); for(int i=0; i<10010; i++){ num[i]=INF; } num[1]=1; int n; cin >> n; queue q; q.push(1); while(!q.empty()){ int now=q.front(); q.pop(); if(flag[now]){ continue; } flag[now]=true; int d=cnt(now); if(1<=now-d && now-d<=n && flag[now-d]==false){ num[now-d]=min(num[now]+1, num[now-d]); q.push(now-d); } if(1<=now+d && now+d<=n && flag[now+d]==false){ num[now+d]=min(num[now]+1, num[now+d]); q.push(now+d); } } if(flag[n]){ cout << num[n] << endl; } else { cout << -1 << endl; } return 0; }