N = int(input()) ans = -1 q = [1] r = set([]) for i in range(1, N+1): if q == [] : break l = [] for j in q: if j == N: ans = i l = [] break elif not j in r: r.add(j) b = bin(j).count('1') n, m = j+b, j-b if n <= N : l += [n] if m > 0 : l += [m] q = l print(ans)