using System; using System.Linq; using System.Collections.Generic; namespace yukicoder{ class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var list = new Queue();//幅優先探索。探索保留場所 var moveMin = new int[N];//最短移動数保持。また、探索済みかどうか。 var movePlace = new int[N];//移動前の場所 for(int i = 0;i < N;i++) { movePlace[i] = -1; moveMin[i] = -1; } moveMin[0] = 1; list.Enqueue(1); while(list.Count > 0 && moveMin[N - 1] == -1) { int explore = list.Dequeue(); int bitNum = bitCul(explore); int next = explore + bitNum; if(next <= N && moveMin[next - 1] == -1) { moveMin[next - 1] = moveMin[explore - 1] + 1; movePlace[next - 1] = explore; list.Enqueue(next); } next = explore - bitNum; if(next > 0 && moveMin[next -1] == -1) { moveMin[next - 1] = moveMin[explore - 1] + 1; movePlace[next - 1] = explore; list.Enqueue(next); } } Console.WriteLine(moveMin[N-1]); } static int bitCul(int n) { var list = new List(); while(n > 0) { if(n % 2 == 1) { list.Add(1); }else { list.Add(0); } n /= 2; } return list.Sum(); } } }