import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); if (N == 1) { System.out.println(0); return; } int[] map = new int[N+1]; for (int i = 1; i <= N; i++) { map[i] = Integer.bitCount(i); } boolean[] comp = new boolean[N+1]; for (int i = 1; i < N; i++) { int p = i; while(p < N) { if (p+map[p] == N) { comp[i] = true; } p += map[p]; } } boolean bad = true; for (boolean flg : comp) { if(flg) bad = false; } if (bad) { System.out.println(-1); return; } int[] dp = new int[N+1]; Arrays.fill(dp, Integer.MAX_VALUE-500); int count = 0; for (int i = 1; i <= N; i+=map[i]) { count++; dp[i] = count; } boolean changed = true; while(changed) { changed = false; for (int i = 1; i <=N; i++) { if (i-map[i] > 0 && dp[i-map[i]] > dp[i]+1) { dp[i-map[i]] = dp[i]+1; changed = true; } if (i+map[i] <= N && dp[i+map[i]] > dp[i]+1) { dp[i+map[i]] = dp[i]+1; changed = true; } } } System.out.println(dp[N]); } }