using System; using System.Collections.Generic; using System.Linq; using static System.Console; class Program { static void Main() { var N = int.Parse(ReadLine()); var g = new List[N]; for (int i = 0; i < N; i++) { g[i] = new List(); } for (int i = 0; i < N; i++) { var r = BitCount(i + 1); if (i - r >= 0) g[i].Add(i - r); if (i + r < N) g[i].Add(i + r); } WriteLine(BFS(g)); } static int BitCount(int n) { n = (n & 0x55555555) + (n >> 1 & 0x55555555); n = (n & 0x33333333) + (n >> 2 & 0x33333333); n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f); n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff); return (n & 0x0000ffff) + (n >> 16 & 0x0000ffff); } static int BFS(List[] graph) { var q = new Queue(); q.Enqueue(0); var dist = new int[graph.Length]; dist[0]=1; while (q.Count != 0) { var v = q.Dequeue(); if (v == graph.Length - 1) return dist[graph.Length - 1]; foreach (var vertex in graph[v]) { if (dist[vertex]==0) { dist[vertex] = dist[v] + 1; q.Enqueue(vertex); } } } return -1; } }