using System; using System.Collections.Generic; using System.Linq; namespace No003_ビットすごろく { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] box = Enumerable.Repeat(-1, N + 1).ToArray(); Queue que = new Queue(); box[1] = 1; que.Enqueue(1); while (que.Count != 0) { int pos = que.Dequeue(); int bit = BitNumber(pos); if (pos - bit > 0 && box[pos - bit] == -1) { box[pos - bit] = box[pos] + 1; que.Enqueue(pos - bit); } if (pos + bit <= N && box[pos + bit] == -1) { box[pos + bit] = box[pos] + 1; que.Enqueue(pos + bit); } } Console.WriteLine(box[N]); } static int BitNumber(int a) { int temp = 0; do { temp += a % 2; a /= 2; } while (a > 0); return temp; } } }