import sys, queue class Walker(): def __init__(self, num): self.num = num def getNum(self): return self.num def getStep(self): return self.step def setStep(self, step): self.step = step def getMovility(self): cnt = 0 bin_num = bin(self.num)[2:] for i in bin_num: if int(i): cnt += 1 return cnt n = int(input()) q = queue.Queue() current = 1 movility = 1 step = 1 visited = [False for i in range(n)] while True: if current == n: print(step) break if current - movility > 0 and not visited[current - movility - 1]: w = Walker(current - movility) w.setStep(step + 1) q.put(w) visited[current - movility - 1] = True if current + movility <= n and not visited[current + movility - 1]: w = Walker(current + movility) w.setStep(step + 1) q.put(w) visited[current + movility - 1] = True if q.empty(): print(-1) break w = q.get() current = w.getNum() step = w.getStep() movility = w.getMovility()