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