n = int(input()) bn = [format(x, 'b').count('1') for x in range(n + 1)] l = [9999999] * (n + 1) l[1] = 1 tmp = [1] while tmp: next_tmp = [] for i in tmp: for j in (i - bn[i], i + bn[i]): if 0 < j <= n: if l[j] > l[i] + 1: l[j] = l[i] + 1 next_tmp.append(j) tmp = next_tmp print(l[n] if l[n] < 9999999 else -1)