using System; using System.Collections.Generic; using System.Linq; namespace No3 { class Program { private static int N; private static int[] map; static void Main(string[] args) { N = int.Parse(Console.ReadLine()); Width(); } static void Deep() { map = Enumerable.Repeat(-1, N).ToArray(); Rec(0, 0); Console.Write(map.Last()); } static void Rec(int index, int preventCount) { if (index < 0) return; if (index >= N) return; if (map[index] != -1 && map[index] <= preventCount + 1) return; map[index] = preventCount + 1; if (index == N - 1) return; var bitcount = BitCount(index + 1); Rec(index + bitcount, preventCount + 1); Rec(index - bitcount, preventCount + 1); } static int BitCount(int value) { var count = 0; for (var i = 0; i < 32; i++) { var tmp = 1 << i; if ((value & tmp) != 0) count++; } return count; } static void Width() { map = Enumerable.Repeat(-1, N).ToArray(); map[0] = 1; var queue = new Queue(new[] { 0 }); Func enqueue = (next, current) => { if (next < 0) return false; if (next >= N) return false; if (map[next] != -1) return false; map[next] = map[current] + 1; queue.Enqueue(next); return true; }; while (queue.Any()) { var current = queue.Dequeue(); if (current == N - 1) continue; var bitcount = BitCount(current + 1); enqueue(current + bitcount, current); enqueue(current - bitcount, current); } Console.Write(map.Last()); } } }