N = int(input()) checked = list(-1 for i in range(N + 1)) checked[0] = 0 move = list(map(bin, range(N))) move = list(move[i].count('1') for i in range(N)) node = [] count = 1 pos = 1 def play(): global count global pos checked[pos] = count def moving(): global count global pos if pos + move[pos] <= N: pos += move[pos] count += 1 play() elif pos + move[pos] > N: if node == []: print(-1) else: pos = node.pop() count = checked[pos] play() if pos == N: print(count) elif checked[pos - move[pos]] == -1: checked[pos - move[pos]] = count + 1 node.append(pos - move[pos]) moving() else: moving() play()