from collections import deque n = int(input()) reach = [float("Inf")]*(n+1) reach[1] = 1 q = deque([]) q.append(1) while q: x = q.popleft() c = bin(x).count("1") if 1 <= x-c and reach[x-c] > reach[x] + 1: q.append(x-c) reach[x-c] = reach[x] + 1 if x+c <= n and reach[x+c] > reach[x] + 1: q.append(x+c) reach[x+c] = reach[x] + 1 if reach[-1] >= 10**18: print(-1) else: print(reach[-1])