import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.PriorityQueue; import java.util.Collection; import java.util.Scanner; import java.util.Set; import java.util.HashMap; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Queue; import java.util.Comparator; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, Scanner in, PrintWriter out) { final int n = in.nextInt(); Vertex[] vertices = new Vertex[n]; for (int i = 0; i < n; i++) vertices[i] = new Vertex(); for (int i = 0; i < n; i++) { int f = Integer.bitCount(i + 1); if (i + f < n) vertices[i].setEdge(vertices[i + f], 1); if (i - f >= 0) vertices[i].setEdge(vertices[i - f], 1); } out.println(Search.dijkstra(vertices[0], vertices[n - 1])); } } static class Vertex { private int value; private List edges = new ArrayList(); public Vertex() { } public Vertex(int v) { this.value = v; } public void setEdge(Vertex v, int cost) { this.edges.add(new Edge(v, cost)); } public int getDegree() { return this.edges.size(); } public Edge getEdge(int index) { return this.edges.get(index); } } static class Edge { private Vertex vertex; private int cost; public Edge() { } public Edge(Vertex v) { this.vertex = v; } public Edge(Vertex v, int c) { this.vertex = v; this.cost = c; } public Vertex getVertex() { return this.vertex; } public int getCost() { return this.cost; } } static class Search { public static int dijkstra(Vertex start, Vertex goal) { Map cost = new HashMap<>(); cost.put(start, 1); // initialize priority queue of vertices ordered by cost Comparator costComparator = (Vertex o1, Vertex o2) -> ((cost.get(o1) > cost.get(o2)) ? 1 : 0); Queue vertexQ = new PriorityQueue<>(costComparator); vertexQ.add(start); Set visited = new HashSet<>(); while (vertexQ.size() > 0) { Vertex cv = vertexQ.poll(); if (cv == goal) return cost.get(cv); // found shortest path visited.add(cv); // see every connected edges for (int i = 0; i < cv.getDegree(); i++) { Vertex nextV = cv.getEdge(i).getVertex(); if (visited.contains(nextV)) continue; int nextCost = cost.get(cv) + cv.getEdge(i).getCost(); // if new cost is smaller, replace it if (nextCost < cost.getOrDefault(nextV, Integer.MAX_VALUE)) { cost.put(nextV, nextCost); if (!vertexQ.contains(nextV)) vertexQ.add(nextV); } } } return -1; // unreachable } } }