public class Main { public static void main(String[] args) { int n = new java.util.Scanner(System.in).nextInt(); new Main(n).solve(); } private int n; Main(int n) { this.n = n; } void solve() { System.out.println(path(n)); } int bits(int n) { int c = 0; while (n > 0) { c += n % 2; n /= 2; } return c; } int path(int n) { boolean[] visited = new boolean[n]; int len = 64; short[] q = new short[len]; short[] depth = new short[len]; int head = 0; int tail = 0; q[0] = 1; depth[0] = 1; tail++; while (head != tail) { short h = q[head]; short d = depth[head]; head = (head + 1) % len; if (h == n) { return d; } if (h < 1 || n < h || visited[h - 1]) { continue; } visited[h - 1] = true; int b = bits(h); q[tail] = (short)(h + b); depth[tail] = (short)(d + 1); tail = (tail + 1) % len; if (head == tail) { throw new RuntimeException(); } q[tail] = (short)(h - b); depth[tail] = (short)(d + 1); tail = (tail + 1) % len; if (head == tail) { throw new RuntimeException(); } } return -1; } }