import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; import java.util.ArrayList; /** * 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 { static int dfs(Task.Vertex[] v, Task.Vertex cv, int count, int min) { int cCount = count + 1; if (cCount > min || cCount > v.length + 1) return min; int cMin = min; for (int i = 0; i < cv.getDegree(); i++) { if (cv.getEdge(i).getVertex() == v[v.length - 1]) { return cCount + 1; } else cMin = Math.min(dfs(v, cv.getEdge(i).getVertex(), cCount, cMin), cMin); } return cMin; } public void solve(int testNumber, Scanner in, PrintWriter out) { final int n = in.nextInt(); Task.Vertex[] v = new Task.Vertex[n]; for (int i = 0; i < n; i++) v[i] = new Task.Vertex(); for (int i = 0; i < n; i++) { String s = Integer.toBinaryString(i + 1); int len = 0; for (char c : s.toCharArray()) if (c == '1') len++; if (i + len < n) { v[i].setEdge(v[i + len]); } if (i - len >= 0) { v[i].setEdge(v[i - len]); } } int min = dfs(v, v[0], 0, Integer.MAX_VALUE); min = min == Integer.MAX_VALUE ? -1 : min; out.println(min); } static class Vertex { private ArrayList edges = new ArrayList(); Vertex() { } void setEdge(Task.Vertex v) { this.edges.add(new Task.Edge(v)); } int getDegree() { return this.edges.size(); } Task.Edge getEdge(int index) { return this.edges.get(index); } } static class Edge { private Task.Vertex v; private int cost; Edge() { this.v = v; } Edge(Task.Vertex v) { this.v = v; } Edge(Task.Vertex v, int c) { this.v = v; this.cost = c; } Task.Vertex getVertex() { return this.v; } } } }