from collections import deque def read_int(): return int(input()) def read_int_list(): return list(map(int, input().split())) N = read_int() costs = [10000000] * (N + 1) used = [False] * (N + 1) costs[1] = 1 q = deque() q.append(1) while q: v = q.popleft() if used[v]: continue used[v] = True bits = bin(v).count('1') g = [v - bits, v + bits] for i in g: if i > N: continue if costs[i] > costs[v] + 1: costs[i] = costs[v] + 1 q.append(i) print(-1 if costs[N] == 10000000 else costs[N])