import java.util.ArrayDeque; import java.util.Arrays; import java.util.Scanner; // 解説AC class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] d = new int[N + 1]; final int INF = Integer.MAX_VALUE / 2; Arrays.fill(d, INF); d[1] = 1; ArrayDeque deque = new ArrayDeque(N * 2); //適当な初期容量 deque.add(1); while(deque.isEmpty() == false) { int nowPosition = deque.poll(); int move = Integer.bitCount(nowPosition); for(int i = -move; i <= move; i += move *2) { int nextPosition = nowPosition + i; if(nextPosition < 1 || nextPosition > N) continue; if(d[nextPosition] != INF) continue; d[nextPosition] = d[nowPosition] + 1; deque.add(nextPosition); } } if(d[N] == INF) System.out.println(-1); else System.out.println(d[N]); } }