#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define REP(i,n) for(ll i=0; i<(n); ++i) #define TEN(x) ((ll)1e##x) #define ALL(v) (v).begin(), (v).end() namespace std { bool operator<(pair> lhs, pair> rhs) { return (lhs.first != rhs.first) ? lhs.first < rhs.first : lhs.second.to_ulong() < rhs.second.to_ulong(); } } int main() { ll n; cin >> n; bitset<14> goal = n; vector visited(n+1, false); priority_queue>> que; que.push({1, 1}); while (!que.empty()) { ll d; bitset<14> current; tie(d,current) = que.top(); que.pop(); if (current == goal) { cout << d << endl; return 0; } if (visited[current.to_ulong()]) continue; visited[current.to_ulong()] = true; auto c = current.count(); if (current.to_ulong() - c >= 1) que.push({d+1, current.to_ulong() - c}); if (current.to_ulong() + c <= goal.to_ulong()) que.push({d+1, current.to_ulong() + c}); } cout << -1 << endl; return 0; }