from collections import deque n = int(input()) inf = 1 << 30 dist = [inf] * (n + 1) dist[1] = 1 d = deque([1]) while d: u = d.popleft() x = bin(u).count("1") if u + x <= n and dist[u+x] > dist[u] + 1: dist[u+x] = dist[u] + 1 d.append(u+x) if 0 < u - x and dist[u-x] > dist[u] + 1: dist[u-x] = dist[u] + 1 d.append(u-x) print(dist[n] if dist[n] < inf else -1)