import java.util.Scanner; public class Main { static boolean memo[]; static int ans, min; public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n = sc.nextInt(); int map[] = new int[n + 1]; int cnt; String bin; for (int i = 1; i <= n; i++) { cnt = 0; bin = Integer.toBinaryString(i); for (int j = 0; j < bin.length(); j++) { if (bin.charAt(j) == '1') { cnt++; } } map[i] = cnt; } memo = new boolean[n + 1]; memo[1] = true; ans = 0; min = Integer.MAX_VALUE; solve(map, 1, n); if (min == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(min + 1); } } private static void solve(int[] map, int s, int n) { if (s == n) { min = Math.min(min, ans); return; } if (s + map[s] <= n && !memo[s + map[s]]) { memo[s + map[s]] = true; ans++; solve(map, s + map[s], n); ans--; memo[s + map[s]] = false; } if (s - map[s] > 0 && !memo[s - map[s]]) { memo[s - map[s]] = true; ans++; solve(map, s - map[s], n); ans--; memo[s - map[s]] = false; } } }