import java.util.ArrayDeque; import java.util.Queue; import java.util.Scanner; class Pair { public Pair(int pos, int step) { this.pos = pos; this.step = step; } int pos; int step; } 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(new Pair(1, 1)); while (!q.isEmpty()) { Pair p = q.poll(); visited[p.pos] = true; if (p.pos == N) { System.out.println(p.step); return; } char[] binary = Integer.toBinaryString(p.pos).toCharArray(); int move = 0; for (int i = 0; i < binary.length; i++) { if (binary[i] == '1') { move++; } } if (p.pos + move <= N && !visited[p.pos + move]) { q.add(new Pair(p.pos + move, p.step + 1)); } if (p.pos - move > 0 && !visited[p.pos - move]) { q.add(new Pair(p.pos - move, p.step + 1)); } } System.out.println(-1); } }