#include using namespace std; typedef long long ll; #define FOR(i,a,b) for(int i=(a);i<(int)(b);i++) #define rep(i,n) FOR(i,0,n) #define ALL(x) (x).begin(),(x).end() #define pb push_back #define mp make_pair #define F first #define S second int N; bool visited[10010]; int calc(int n) { int res = 0; while(n) { if(n & 1) res++; n >>= 1; } return res; } int main() { memset(visited,0,sizeof(visited)); cin >> N; queue> Q; Q.push(mp(1,1)); while(Q.size()) { auto tmp = Q.front(); if(tmp.F == N) { cout << tmp.S << "\n"; return 0; } visited[tmp.F] = true; int mv = calc(tmp.F); int nxt = tmp.F + mv; if(!visited[nxt] and 1<=nxt and nxt < N) Q.push(mp(nxt, tmp.S+1)); nxt = tmp.F - mv; if(!visited[nxt] and 1<=nxt and nxt < N) Q.push(mp(nxt, tmp.S+1)); Q.pop(); } cout << -1 << "\n"; return 0; }