package yukicoder; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.StringTokenizer; public class P3 { static long[] dist; static long INF = 100000000000000000L; static ArrayList> adj; static class Edge { int to; long w; Edge(int _to, long _w) { this.to = _to; this.w = _w; } @Override public String toString() { return "(to=" + to + ", w=" + w + ")"; } } static class NextNode { int id; long cost; NextNode(int _id, long _cost) { id = _id; cost = _cost; } } static void initG(int V) { // V: the number of vertexes for(int i=0; i()); } } static void dijkstra(int s) { Arrays.fill(dist, INF); dist[s] = 0; PriorityQueue pq = new PriorityQueue(new Comparator() { public int compare(NextNode n1, NextNode n2) { if(n1.cost > n2.cost) return 1; else if(n1.cost==n2.cost) return 0; else return -1; } }); pq.add(new NextNode(s, 0)); while(!pq.isEmpty()) { NextNode cn = pq.poll(); if(cn.cost > dist[cn.id]) continue; for(Edge e : adj.get(cn.id)) { if(cn.cost + e.w < dist[e.to]) { dist[e.to] = cn.cost + e.w; pq.add(new NextNode(e.to, dist[e.to])); } } } } public static void main(String[] args) throws IOException { MyScanner sc = new MyScanner(System.in); int n = sc.nextInt(); dist = new long[n+1]; adj = new ArrayList>(n+1); initG(n+1); for(int i=1; i<=n; i++) { int d = digit(i); if(0<=i+d && i+d<=n) adj.get(i).add(new Edge(i+d, 1)); if(0<=i-d && i-d<=n) adj.get(i).add(new Edge(i-d, 1)); } dijkstra(1); if(dist[n]>=INF) { System.out.println(-1); } else { System.out.println(dist[n]+1); } } public static int digit(int n) { int ret = 0; while(n>0) { if(n%2==1) ret++; n/=2; } return ret; } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner(InputStream s) { br=new BufferedReader(new InputStreamReader(s)); } public String nextLine() throws IOException { return br.readLine(); } public String next() throws IOException { while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }