from collections import deque import sys N = int(input()) q = deque([1]) d_min = [1] + [sys.maxsize] * (N - 1) while(len(q) != 0): now = q.popleft() backword = now - bin(now).count('1') forword = now + bin(now).count('1') if backword > 0 and d_min[backword - 1] > d_min[now - 1] + 1: d_min[backword - 1] = d_min[now - 1] + 1 q.append(backword) if forword <= N and d_min[forword - 1] > d_min[now - 1] + 1: d_min[forword - 1] = d_min[now - 1] + 1 q.append(forword) print(d_min[-1] if d_min[-1] != sys.maxsize else -1)