# -*- coding: utf-8 -*- """ No.3 ビットすごろく https://yukicoder.me/problems/no/3 """ import sys from sys import stdin from functools import lru_cache from heapq import heappop, heappush input = stdin.readline @lru_cache(maxsize=None) def pop_count7(i): ans = 0 while i: ans += 1 i &= (i - 1) return ans def pop_count(i): upper = i >> 7 lower = i % 128 ans = pop_count7(upper) + pop_count7(lower) return ans def dijkstra(adj, s): # ダイクストラ法で始点からの距離を計算する WHITE, GRAY, BLACK = 0, 1, 2 color = [WHITE] * len(adj) d = [float('inf')] * len(adj) # 始点からの距離 計算結果 d[s] = 0 # 始点から自身までの距離は0 pq = [] heappush(pq, (0, s)) while pq: cost, u = heappop(pq) color[u] = BLACK if d[u] < cost: continue for v, cost in adj[u]: if color[v] == BLACK: continue if d[v] > d[u] + cost: d[v] = d[u] + cost heappush(pq, (d[v], v)) color[v] = GRAY return d # 計算した距離情報を返す def main(args): N = int(input()) adj = [[] for _ in range(N+1)] # 隣接リスト for i in range(1, N+1): # 1〜Nそれぞれについて、直接到達できるマスを調べてコスト1の有向グラフにする b = pop_count(i) if 1<= i-b <= N: adj[i].append([i-b, 1]) if 1<= i+b <= N: adj[i].append([i+b, 1]) dist = dijkstra(adj, 1) # 1からの最短移動数を計算 if dist[-1] != float('inf'): print(dist[-1] + 1) # 開始のマスも移動数にカウントするので+1する else: print(-1) if __name__ == '__main__': main(sys.argv[1:])