#include #include #include using namespace std; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } vectorseen; int dfs(int now, int target, int count) { if (now == target) { return count; } seen[now] = true; int b_c = __builtin_popcount(now); int s = seen.size(); int tmp = 100000; bool can_do = false; if (s > now+b_c && !seen[now+b_c]) { tmp = min(tmp, dfs(now+b_c, target, count+1)); can_do = true; } if (now-b_c>0 && !seen[now-b_c]) { can_do = true; int n = dfs(now-b_c, target, count+1); if (tmp == -1) { tmp = n; } else if (n == -1 && tmp == 10000) { tmp = -1; } else { tmp = min(tmp, n); } } if (!can_do) { tmp = -1; } return tmp; } int main() { int N; cin >> N; seen.resize(N+1); int count = dfs(1, N, 1); cout << count << endl; return 0; }