import heapq n = int(input()) lst1 = [] for i in range(1,100001): dig = len(bin(i))-2 res = 0 for j in range(dig): if i >> j & 1: res += 1 lst1.append(res) visited = [0]*n visited[0] = 1 queue = [(1,0)] #res,now ans = -1 heapq.heapify(queue) while queue: res,now = heapq.heappop(queue) if now == n-1: #最小から取り出してるので、初めに行き着いたやつが答え ans = res break if 0 <= now-lst1[now] and visited[now-lst1[now]] == 0: visited[now-lst1[now]] = 1 heapq.heappush(queue,(res+1,now-lst1[now])) if now+lst1[now] < n and visited[now+lst1[now]] == 0: visited[now+lst1[now]] = 1 heapq.heappush(queue,(res+1,now+lst1[now])) print(ans)