package yukicoder; import java.util.LinkedList; import java.util.Scanner; public class N3 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); LinkedList queue = new LinkedList(); LinkedList visited = new LinkedList(); int i = 1; int a, b, dist; for(int j = 0;j <= n;++j){ visited.add(0); } while(true){ dist = visited.get(i); a = i - countBin(i); b = i + countBin(i); if(a > 0 && a <= n && visited.get(a) == 0){ visited.set(a, dist + 1); queue.offer(a); } if(b > 0 && b <= n && visited.get(b) == 0){ visited.set(b, dist + 1); queue.offer(b); } if(a == n || b == n){ System.out.println(dist + 2); return; } if(queue.size() == 0){ System.out.println(-1); return; } i = queue.poll(); } } public static int countBin(int x){ int i = 0; while(x != 0){ i += x % 2; x /= 2; } return i; } }