import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Here your code ! Scanner sc = new Scanner(System.in); int N = sc.nextInt(); Queue q = new ArrayDeque<>(); String str; int[] mas = new int[N]; Arrays.fill(mas, -1); int now, next, prev; int count, result; mas[0] = 1; q.offer(1); while(!q.isEmpty()){ now = q.poll(); if(now == N){ //result = mas[N-1]; break; } str = Integer.toBinaryString(now); count = 0; for(char x: str.toCharArray()){ if(x == '1'){ count++; } } next = now + count; if(next <= N && mas[next-1] == -1){ mas[next-1] = mas[now-1] +1; q.offer(next); } prev = now - count; if(prev > 0 && mas[prev-1] == -1){ mas[prev-1] = mas[now-1] +1; q.offer(prev); } } /*for(int i=0; i