#include #define REP(i, x, n) for(int i = x; i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define F first #define S second #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair P; const int INF = 1 << 29; int main() { // ios_base::sync_with_stdio(false); int N; cin >> N; vector minCost(N + 1, INF); minCost[1] = 1; queue Q; Q.push(1); while(!Q.empty()) { int pos = Q.front(); Q.pop(); int cnt = __builtin_popcount(pos); if(pos == N) { cout << minCost[pos] << endl; return 0; } if(pos + cnt <= N && minCost[pos + cnt] > minCost[pos] + 1) { minCost[pos + cnt] = minCost[pos] + 1; Q.push(pos + cnt); } if(pos - cnt > 0 && minCost[pos - cnt] > minCost[pos] + 1) { minCost[pos - cnt] = minCost[pos] + 1; Q.push(pos - cnt); } } cout << "-1" << endl; return 0; }