def can_go(n): return sum(map(int,list(n))) N=int(input()) m=dict(zip(range(1,N+1),list(map(format,range(1,N+1),N*"b")))) queue=[1] Visited=[]#訪問済み cost=[float("inf") for i in range(N)] cost[0]=1 next_target=[] goal=N ##print(m,goal) while(1): if len(queue)==0: break target=queue[0] if target==goal: break next_target=[target+can_go(m[target]),target-can_go(m[target])] for i in next_target: if 1<=i and i<=N and not i in queue and not i in Visited: queue.append(i) cost[i-1]=cost[target-1]+1 queue.pop(0) Visited.append(target) ## print(queue,cost) if cost[-1]!=float("inf"): print(cost[N-1]) else : print(-1)