N = int(input()) queue = [1] visited = set() count = 0 while queue: count += 1 n = [] flg = False for i in queue: if i == N: print(count) flg = True break b1_num = bin(i).count('1') a = i + b1_num b = i - b1_num if a <= N and a not in visited: visited.add(a) n.append(a) if 1 < b and b not in visited: visited.add(b) n.append(b) queue = n if(flg): break else: print(-1)