import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; import java.util.ArrayDeque; 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 { 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]); } } ArrayDeque vq = new ArrayDeque<>(); v[0].setValue(1); vq.add(v[0]); int count = 1; while (count < n) { int m = vq.size(); for (int j = 0; j < m; j++) { Task.Vertex cv = vq.poll(); for (int i = 0; i < cv.getDegree(); i++) { Task.Vertex nv = cv.getEdge(i).getVertex(); if (nv == v[n - 1]) { count++; out.println(count); return; } else { if (!nv.hasValue()) { vq.add(nv); nv.setValue(1); } } } } count++; } out.println(-1); } static class Vertex { private int value; private boolean hasValue = false; private ArrayList edges = new ArrayList(); Vertex() { } void setValue(int v) { this.value = v; this.hasValue = true; } 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); } boolean hasValue() { return this.hasValue; } } 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; } } } }