#!/mnt/c/Users/moiki/bash/env/bin/python # N,M = map(int, input().split()) N = int(input()) dp = [10**9 for _ in range(N+1)] dp[1] = 1 stack = [1] while stack: i = stack.pop() cnt = bin(i).count("1") if 1 <= i-cnt <= N: if dp[i]+1 < dp[i-cnt] : if not (i-cnt) in stack: stack.append( i-cnt ) dp[i-cnt] = dp[i]+1 if 1 <= i+cnt <= N: if dp[i]+1 < dp[i+cnt] : if not (i+cnt) in stack: stack.append( i+cnt ) dp[i+cnt] = dp[i]+1 print(dp[N] if dp[N] != 10**9 else -1)