import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import static java.lang.System.in; import static java.lang.System.setOut; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); int N = Integer.parseInt(reader.readLine()); int[] table = new int[N + 1]; for (int i = 0; i <= N; i++) { table[i] = Integer.MAX_VALUE; } Queue queue = new LinkedList<>(); Cell first = new Cell(1, 1); queue.add(first); boolean[] visited = new boolean[N + 1]; while (!queue.isEmpty()) { Cell cell = queue.poll(); if (cell.position == N) { } else { int numOfOne = Integer.bitCount(cell.position); int nextPosition = cell.position + numOfOne; int prevPosition = cell.position - numOfOne; if (canMove(nextPosition, N, visited)) { queue.add(new Cell(nextPosition, cell.cost + 1)); visited[nextPosition] = true; } if (canMove(prevPosition, N, visited)) { queue.add(new Cell(prevPosition, cell.cost + 1)); visited[prevPosition] = true; } } table[cell.position] = Math.min(table[cell.position], cell.cost); } if (table[N] != Integer.MAX_VALUE) { System.out.println(table[N]); } else { System.out.println(-1); } } private static boolean canMove(int position, int N, boolean[] visited) { if (position < 1 || position >= N + 1 || visited[position]) { return false; } else { return true; } } } class Cell { int position; int cost; public Cell(int position, int cost) { this.position = position; this.cost = cost; } @Override public String toString() { return "Cell{" + "position=" + position + ", cost=" + cost + '}'; } }