#include #include #include #include using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) bool flag[10010]; int kaisuu[10010]; int bitcnt(int now){ int ans = 0;//nowを2進数にしたときに、いくつbitが立っているか while(now > 0){ if(now % 2 == 1) ans++; now /= 2; } return ans; } int main(void){ rep(i, 10010) flag[i] = false; rep(i, 10010) kaisuu[i] = -1; kaisuu[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 = bitcnt(now); if(1 <= now - d && now - d <= n && flag[now - d] == false && kaisuu[now - d] == -1){ kaisuu[now - d] = kaisuu[now] + 1; q.push(now - d); } if(1 <= now + d && now + d <= n && flag[now + d] == false && kaisuu[now + d] == -1){ kaisuu[now + d] = kaisuu[now] + 1; q.push(now + d); } } if(flag[n]) printf("%d\n", kaisuu[n]); else printf("-1\n"); }