using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); int[] ds = Enumerable.Repeat(-1, n + 1).ToArray(); Queue q = new Queue(); ds[1] = 1; q.Enqueue(1); while (0 < q.Count) { int p = q.Dequeue(); int d = Convert.ToString(p, 2).Count(x => x == '1'); if (0 < p - d && ds[p - d] == -1) { ds[p - d] = ds[p] + 1; q.Enqueue(p - d); } if (p + d <= n && ds[p + d] == -1) { ds[p + d] = ds[p] + 1; q.Enqueue(p + d); } } Console.WriteLine(ds[n]); } }