//test.cpp #include #include #include #include #define REP(i,n) for(int i=0;i<=n;i++) using namespace std; typedef unsigned long ul; typedef pair P; int bit(int i){ int cnt=0; while(i>0){ cnt += i%2; i /= 2; } return cnt; } int main() { int N,INF = 10001,cnt = -1; int sg[10000],ms[10001]; scanf("%d",&N); REP(i,N){sg[i] = bit(i);ms[i]=INF;} queue q; q.push(1); ms[1] = 1; while (!q.empty()) { int now = q.front(); if(now == N){ cnt = ms[now]; break; } if(0 < now - sg[now] && ms[now - sg[now]] == INF){ q.push(now - sg[now]); ms[now - sg[now]] = ms[now] + 1; } if(now + sg[now] <= INF && ms[now + sg[now]] == INF){ q.push(now + sg[now]); ms[now + sg[now]] = ms[now] + 1; } q.pop(); } if(cnt != -1)printf("%d\n", cnt); else printf("-1\n"); return 0; }