N = input() d = [] used = [] for i in range(10001): d.append(bin(i).count("1")) used.append(0) que = [(1,1)] ans = -1 while que: pos,cnt = que.pop(0) if pos == N: ans = cnt break left = pos-d[pos] right = pos + d[pos] if left>0 and not used[left]: used[left] = 1 que.append((left,cnt+1)) if right < N+1 and not used[right]: used[right] = 1 que.append((right,cnt+1)) print ans