import heapq def bitsugoroku(N,cur=1,path=None): h = [] completed=[] heapq.heappush(h,[N,cur,[]]) while len(h) > 0: h.sort(key=lambda x:len(x[2])) N,cur,path = heapq.heappop(h) if cur not in completed: path.append(cur) completed.append(cur) if N == cur: return len(path) a = str(bin(cur)).count("1")#移動する数 if (cur + a <= N) and ((cur + a) not in completed): heapq.heappush(h,[N,cur+a,path.copy()]) if (cur - a > 1) and ((cur -a) not in completed): heapq.heappush(h,[N,cur-a,path.copy()]) return -1 N = int(input()) print(bitsugoroku(N))