#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(int)(b);i++) #define rep(i,n) FOR(i,0,n) #define ZERO(a) memset(a,0,sizeof(a)) const int INF = 114514810; int cost[10010][10010]; bool used[10010]; int d[10010]; int n; int calc(int n) { int res = 0; while(n) { if(n & 1) res++; n >>= 1; } return res; } void dijkstra(int s) { ZERO(used); fill(d,d+n+1,INF); d[s] = 0; while(1) { int v = -1; FOR(u,1,n+1) { if(!used[u] and (v == -1 or d[u] < d[v])) v = u; } if(v == -1) break; used[v] = 1; FOR(u,1,n+1) d[u] = min(d[u], d[v] + cost[v][u]); } } int main() { cin>>n; rep(i,n+1) rep(j,n+1) cost[i][j] = INF; vector a(n+1); rep(i,n+1) a[i] = calc(i); FOR(i,1,n+1) { if(i + a[i] <= n) { cost[i][i+a[i]] = 1; } else if(i - a[i] >= 1) { cost[i][i-a[i]] = 1; } } dijkstra(1); cout << (d[n]>=INF ? -1 : d[n] + 1) << "\n"; return 0; }