import java.util.ArrayDeque; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); boolean[] visited = new boolean[N + 1]; Queue q = new ArrayDeque<>(); q.add(1); int step = 1; while (!q.isEmpty()) { int pos = q.poll(); visited[pos] = true; if (pos == N) { break; } char[] binary = Integer.toBinaryString(pos).toCharArray(); int move = 0; for (int i = 0; i < binary.length; i++) { if (binary[i] == '1') { move++; } } boolean moved = false; if (pos + move <= N && !visited[pos + move]) { q.add(pos + move); moved = true; } if (pos - move > 0 && !visited[pos - move]) { q.add(pos - move); moved = true; } if (moved) step++; } if (visited[N]) { System.out.println(step); } else { System.out.println(-1); } step++; } }