popcount = lambda n: bin(n).count('1') INF = 1001001001 N = int(input()) pop = [popcount(x) for x in range(N)] dp = [INF] * (N+1) dp[1] = 1 while True: changed = False for i in range(1, N): if dp[i] == INF: continue j = i + pop[i] if j <= N and dp[i] + 1 < dp[j]: dp[j] = dp[i] + 1 changed = True j = i - pop[i] if j >= 0 and i != j and dp[i]+1 < dp[j]: dp[j] = dp[i]+1 changed = True if not changed: break print(-1 if dp[N] == INF else dp[N])