import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Yukicoder3 { static int N; static int[] bitCount = new int[10001]; static boolean[] memo = new boolean[10001]; static int count = 0; static int min = Integer.MAX_VALUE; static boolean found = false; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(br.readLine()); String buf; Arrays.fill(bitCount, 0); for (int i = 1; i <= 10000; i++) { buf = Integer.toBinaryString(i); for (int j = 0; j < buf.length(); j++) { if (buf.charAt(j) == '1') bitCount[i]++; } } // search Arrays.fill(memo, false); count = 1; search(1); if (found) System.out.println(min); else System.out.println(-1); } private static void search(int n) { if (n < 1 || n > N || memo[n]) { count--; return; } memo[n] = true; if (n == N) { found = true; min = Math.min(min, count); return; } count++; search(n + bitCount[n]); count++; search(n - bitCount[n]); } }