#include using namespace std; #define rep(i,n) for(int i=0;i<(n);++i) #define loop for(;;) #define trace(var) cerr<<">>> "<<#var<<" = "<; template inline ostream& operator<<(ostream&os, pair p) { return os << '(' << p.first << ", " << p.second << ')'; } template inline ostream& operator<<(ostream&os, tuple t) { return os << '(' << get<0>(t) << ", " << get<1>(t) << ')'; } template inline ostream& operator<<(ostream&os, tuple t) { return os << '(' << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ')'; } template inline ostream& operator<<(ostream&os, set v) { os << "(set"; for (T item: v) os << ' ' << item; os << ")"; return os; } template inline ostream& operator<<(ostream&os, vector v) { if (v.size() == 0) { return os << "(empty)"; } os << v[0]; for (int i=1, len=v.size(); i inline istream& operator>>(istream&is, vector&v) { rep (i, v.size()) is >> v[i]; return is; } // ^ > v < int dx[] = { -1, 0, 1, 0 }; int dy[] = { 0, 1, 0, -1 }; using vi = vector; using vvi = vector; using vd = vector; using vvd = vector; using vb = vector; int main() { int n; cin >> n; if (n==1){ puts("1");return 0;} vi pc(n); for (int i = 1; i <= n; ++i) { pc[i-1] = __builtin_popcount(i); } vi table(n); rep (i, n) table[i] = inf; table[0] = 1; stack s; s.push(0); while (not s.empty()) { int u = s.top(); s.pop(); vi vs = { u - pc[u], u + pc[u] }; for (int v: vs) { if (v < 0 or v >= n) continue; if (table[v] > table[u] + 1) { table[v] = table[u] + 1; s.push(v); } } } int ans = table[n-1] < inf ? table[n-1] : -1; cout<