#include "bits/stdc++.h" using namespace std; using ll = long long; using ull = unsigned long long; using vb = vector; using vi = vector; using vvi = vector>; using vl = vector; using vvl = vector>; using pii = pair; using pll = pair; #define FOR(i,a,b) for(ll i = (a); i < (ll)(b); i++) #define REP(i,n) FOR(i,0,n) #define ALL(obj) (obj).begin(), (obj).end() #define rALL(obj) (obj).rbegin(), (obj).rend() #define eb(val) emplace_back(val) const double PI = acos(-1); const double EPS = 1e-10; const ll MOD = 1e9+7; const ll INF = 1e10; //#define int ll void cioacc(){//accelerate cin/cout cin.tie(0); ios::sync_with_stdio(false); } struct edge{ int to,cost; }; bool operator>(edge left,edge right){ return left.cost > right.cost; } using vve = vector>; vl dijkstra(vve &gr,int st = 0){ int n = gr.size(); vl dist(n,INF); dist[st] = 0; priority_queue,greater> q; q.push(make_pair(dist[st]=0,st)); while(!q.empty()){ int now = q.top().second; q.pop(); REP(i,gr[now].size()){ edge &e = gr[now][i]; if(dist[now]+e.cost> n; vve gr(n); REP(i,n){ int now = i+1; bitset<32> bits(i+1); int dis = (int)bits.count(); if(now-dis>=1) gr[i].emplace_back(edge{now-1-dis,1}); if(now+dis<=n) gr[i].emplace_back(edge{now-1+dis,1}); } auto dist = dijkstra(gr); cout << (dist[n-1]==INF?-1:dist[n-1]+1) << endl; }