N=int(input()) dp=[float("inf")]*(N+1) dp[1]=1;dp[0]=-1 from collections import deque q=deque([1]) while q: i=q.popleft() if dp[i]>=1: a=str(bin(i)) a=a.count("1") try: if dp[i+a]==float("inf"): q.append(i+a) dp[i+a]=min(dp[i]+1,dp[i+a]) except IndexError: pass try: if dp[i-a]==float("inf"): q.append(i-a) dp[i-a]=min(dp[i]+1,dp[i-a]) except IndexError: pass print(dp[N] if dp[N]!=float("inf") else -1)