N = int(raw_input()) queue = [1,1] path = [False for i in range(N)] ans = -1 path[0] = True while len(queue) > 0: now,count= queue.pop(0),queue.pop(0) if now == N: ans = count break s = str(bin(now)).count('1') if s == 0:continue next1 = now + s next2 = now - s if not(next1 > N or path[next1 - 1]): path[next1 - 1] = True queue.append(next1) queue.append(count + 1) if not(next2 <= 0 or path[next2 - 1]): path[next2 - 1] = True queue.append(next2) queue.append(count + 1) print ans