#include #define rep(i,a,n) for (int i = a; i < n; i++) #define per(i,n,a) for (int i = n - 1; i >= a; i--) using namespace std; #define MAX 9999999 int bitcount(int a){ int ret = 0; while (a > 0){ ret++; a &= a - 1; } return ret; } int main() { int N; cin >> N; vector dp(N + 1, MAX); dp[1] = 1; queue q; q.push(1); while (!q.empty()) { int now = q.front(); q.pop(); int move = bitcount(now); for (int i = -move; i <= move; i += move * 2) { int next = now + i; if (next < 1 || next > N) continue; if (dp[next] != MAX) continue; dp[next] = dp[now] + 1; q.push(next); } } if (dp[N] == MAX) cout << -1 << endl; else cout << dp[N] << endl; }