from collections import deque from sys import stdin, stdout input = lambda: stdin.readline().rstrip() write = stdout.write def bitCount(i): ret = 0 while i: i &= i - 1 ret += 1 return ret def main(): N = int(input()) flag = [None] * (N + 1) flag[1] = 1 que = deque([(1, 1)]) while que: cnt, n = que.popleft() bit = bitCount(n) a, b = n + bit, n - bit if a <= N or b >= 0: cnt += 1 if a <= N and flag[a] is None: que.append((cnt, a)) flag[a] = cnt if b > 0 and flag[b] is None: que.append((cnt, b)) flag[b] = cnt print(-1) if flag[-1] is None else print(flag[-1]) main()