from Queue import Queue n=input() q=Queue() q.put((1,1)) h=[False]*(n+1) while not q.empty(): a,b=q.get() if h[a]: continue h[a]=True if a==n: print b exit() bit=bin(a).count("1") if a+bit<=n: q.put((a+bit,b+1)) if a-bit>0: q.put((a-bit,b+1)) print -1