import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.HashMap; 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(); 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); } BellmanFord bf = new BellmanFord(vertices[0]); if (bf.isReachable(vertices[n - 1])) out.println(1 + bf.getCost(vertices[n - 1])); else out.println(-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 from; private Vertex to; private int cost; public Edge() { } public Edge(Vertex to) { this.to = to; } public Edge(Vertex to, int c) { this.to = to; this.cost = c; } public Edge(Vertex from, Vertex to) { this.to = to; this.from = from; } public Edge(Vertex from, Vertex to, int c) { this.to = to; this.from = from; this.cost = c; } public Vertex getTo() { return this.to; } public int getCost() { return this.cost; } } static class BellmanFord { private Vertex start; private Map cost = new HashMap<>(); private Map route = new HashMap<>(); public BellmanFord(Vertex start) { this.start = start; cost.put(start, 0); findPath(); } private void findPath() { boolean updated = true; while (updated) { updated = false; List vertices = new ArrayList<>(); vertices.addAll(cost.keySet()); for (Vertex cv : vertices) { for (int i = 0; i < cv.getDegree(); i++) { Edge ce = cv.getEdge(i); Vertex tv = ce.getTo(); int cc = cost.get(cv) + ce.getCost(); if (cc < cost.getOrDefault(tv, Integer.MAX_VALUE)) { updated = true; route.put(tv, ce); cost.put(tv, cc); } } } } } public int getCost(Vertex goal) { return cost.get(goal); } public boolean isReachable(Vertex v) { return cost.containsKey(v); } } }