from collections import deque N=int(input()) DP=[1<<30]*(N+1) DP[1]=1 Q=deque([1]) while Q: x=Q.popleft() b=bin(x).count("1") if x+b<=N and DP[x+b]>DP[x]+1: DP[x+b]=DP[x]+1 Q.append(x+b) if x-b<=N and DP[x-b]>DP[x]+1: DP[x-b]=DP[x]+1 Q.append(x-b) if DP[N]==1<<30: print(-1) else: print(DP[N])