n = int(input()) a = [float("inf") for _ in range(n * 2 + 10)] a[1] = 1 b = [0 for _ in range(n * 2 + 10)] b[0] = 1 s = [] s_1 = [1,1] s.append(s_1) while len(s) != 0 and b[n] != 1: s.sort() t = s.pop(0) u = str(bin(t[1])).count("1") b[t[1]] = 1 if t[1] < n: if b[t[1] + u] != 1 and a[t[1]] + 1 < a[t[1] + u]: a[t[1] + u] = a[t[1]] + 1 s_1 = [a[t[1]] + 1 , t[1] + u] s.append(s_1) if b[t[1] - u] != 1 and a[t[1]] + 1 < a[t[1] - u]: a[t[1] - u] = a[t[1]] + 1 s_1 = [a[t[1]] + 1 , t[1] - u] s.append(s_1) if a[n] == float("inf"): print(-1) else: print(a[n])