from heapq import heappop,heappush N = int(input()) D = [True]*(N+1) Q = [(1,1)] ans = -1 while Q: q = heappop(Q) if q[1]==N: ans=q[0] break f,b = q[1]+bin(q[1]).count("1"),q[1]-bin(q[1]).count("1") if f<=N and D[f]: D[f] = False heappush(Q,(q[0]+1,f)) if b>0 and D[b]: D[b] = False heappush(Q,(q[0]+1,b)) print(ans)