import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] arr = new int[n + 1]; Arrays.fill(arr, -1); ArrayDeque deq = new ArrayDeque<>(); ArrayDeque next = new ArrayDeque<>(); deq.add(1); int count = 0; while (deq.size() > 0) { count++; while (deq.size() > 0) { int x = deq.poll(); if (arr[x] == -1) { arr[x] = count; int y = getCount(x); if (x + y <= n) { next.add(x + y); } if (x - y > 0) { next.add(x - y); } } } ArrayDeque tmp = deq; deq = next; next = tmp; } System.out.println(arr[n]); } static int getCount(int x) { int count = 0; while (x > 0) { count += x % 2; x /= 2; } return count; } }