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