def count(n): a=0 num=str(bin(n)) ln=len(num) for i in range(ln): if num[i]=='1': a+=1 return a def breadth(N): q=[[0 for i in range(2)]for j in range(10000)] mile=[0 for i in range(20000)] num=[0 for i in range(2)] q[0][0]=1 mile[1]=1 layer=1 k=0 j=0 i=0 while q!=None: if max(q[i])==0: return -1 if N==1: return 1 k=0 j=0 while layer>j: num[0]=q[i][j]+count(q[i][j]) num[1]=q[i][j]-count(q[i][j]) for n in num: if mile[n]==0 and n<=N: q[i+1][k]=n k+=1 mile[n]=1 if n==N: return i+1+1 j+=1 layer=j i+=1 N=int(input()) print(breadth(N))