#include using namespace std; int countBit(int n) { return __builtin_popcount(n); } int countBit(unsigned int n) { return __builtin_popcount(n); } int countBit(long long n) { return __builtin_popcountll(n); } int countBit(unsigned long long n) { return __builtin_popcountll(n); } struct Edge { typedef int CostType; const static int cost = 1; int from, to; Edge(int from, int to) : from(from), to(to) {}; }; template struct WeightedEdge : public Edge { typedef Cost CostType; Cost cost; WeightedEdge(int from, int to, Cost cost = 0) : Edge(from, to), cost(cost) {} }; template struct ResidualEdge : public Edge { typedef Capacity CapacityType; Capacity cap; int rev; ResidualEdge(int from, int to, Capacity cap) : Edge(from, to), cap(cap) {} ResidualEdge reverse() const {return ResidualEdge(to, from, 0);} }; template struct WeightedResidualEdge : public ResidualEdge { Cost cost; WeightedResidualEdge(int from, int to, Capacity cap, Cost cost) : ResidualEdge(from, to, cap), cost(cost) {} WeightedResidualEdge reverse() const {return WeightedResidualEdge(this->to, this->from, 0, -cost);} }; template class Graph { public: typedef Edge EdgeType; virtual int size() const = 0; template void addEdge(Args...) {} template void addUndirectedEdge(Args...) {} virtual vector getEdges() const = 0; virtual vector getEdges(int from) const = 0; virtual vector getEdges(int from, int to) const = 0; virtual int getDegree(int v) const = 0; }; template class AdjacencyList : public Graph { protected: vector> graph; public: AdjacencyList(int n) : graph(n) {} int size() const { return graph.size(); } template void addEdge(Args... args) { Edge edge(args...); graph[edge.from].emplace_back(edge); } template void addUndirectedEdge(Args... args) { Edge edge(args...); addEdge(edge); swap(edge.from, edge.to); addEdge(edge); } vector getEdges() const { vector res; for (const auto& edges : graph) { res.insert(res.end(), edges.begin(), edges.end()); } return res; } vector getEdges(int from) const { return graph[from]; } vector getEdges(int from, int to) const { vector res; for (const auto& edge : graph[from]) { if (edge.to == to) res.emplace_back(edge); } return res; } int getDegree(int v) const { return graph[v].size(); } vector& operator[](int v) { return graph[v]; } }; template class Search { protected: typedef typename Graph::EdgeType Edge; const Graph graph; vector visited; virtual void push(const State&) = 0; virtual State next() = 0; virtual bool isRunning() = 0; virtual void visit(const State&) {} virtual bool canPruning(const State&) {return false;} public: Search(const Graph& graph) : graph(graph), visited(graph.size(), false) {} void solve(int from) { push(State(from)); while (isRunning()) { State now = next(); int pos = now.getPos(); if (visited[pos]) continue; visited[pos] = true; visit(now); for (const Edge& edge : graph.getEdges(pos)) { State nextState = now.next(edge); if (visited[nextState.getPos()]) continue; if (canPruning(nextState)) continue; push(nextState); } } } bool isReachable(int v) { return visited[v]; } }; template struct BFSState { int pos, prv; BFSState(int pos, int prv = -1) : pos(pos), prv(prv) {} BFSState next(const Edge& edge) const { return BFSState(edge.to, pos); } int getPos() { return pos; } }; template> class BFS : public Search { protected: typedef typename Graph::EdgeType Edge; private: queue que; void push(const State& state) { que.push(state); } State next() { State now = que.front(); que.pop(); return now; } bool isRunning() { return !que.empty(); } public: BFS(const Graph& graph) : Search(graph) {} }; namespace bfs_distance { template class BFSDistance : public BFS { private: typedef BFSState State; void visit(const State& state) { if (state.prv != -1) dis[state.pos] = dis[state.prv] + 1; } public: vector dis; BFSDistance(const Graph& graph) : BFS(graph), dis(graph.size(), 0) {} }; } template inline bfs_distance::BFSDistance bfsDistance(const Graph& graph, int from) { bfs_distance::BFSDistance bfs(graph); bfs.solve(from); return bfs; } int main() { typedef AdjacencyList Graph; int n; cin >> n; Graph graph(n); for (int i = 0; i < n; ++i) { int c = countBit(i + 1); if (i + c < n) graph.addEdge(i, i + c); if (i - c >= 0) graph.addEdge(i, i - c); } auto info = bfsDistance(graph, 0); cout << (info.isReachable(n - 1) ? info.dis[n - 1] + 1 : -1) << endl; }