from collections import * def popcount(n): cnt = 0 while n: cnt += n & 1 n //= 2 return cnt N = int(input()) dist = [-1] * (N + 1) dist[1] = 1 Q = deque([1]) while Q: n = Q.popleft() d = popcount(n) for i in range(-1, 2, 2): if 1 <= n - i * d <= N: if dist[n - i * d] != -1: continue dist[n - i * d] = dist[n] + 1 Q.append(n - i * d) print(dist[N])