#include #include using namespace std; #define MAX 10100 #define INF 1000000007 int N; int dp[MAX + 1]; void dfs(int now) { int cnt = __builtin_popcount(now); int next1 = -1, next2 = -1; if (now + cnt <= N) { if (dp[now] + 1 < dp[now + cnt]) { dp[now + cnt] = dp[now] + 1; next1 = now + cnt; } } if (0 < now - cnt) { if (dp[now] + 1 < dp[now - cnt]) { dp[now - cnt] = dp[now] + 1; next2 = now - cnt; } } if (0 < next1) dfs(next1); if (0 < next2) dfs(next2); } int main() { cin >> N; for (int i = 2; i <= N; i++) dp[i] = INF; dp[1] = 1; dfs(1); cout << (dp[N] == INF ? -1 : dp[N]) << '\n'; return 0; }