using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace foryuki { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] m = new int[N + 1]; m[1] = 1; for (int i = 2; i < N + 1; i++) { m[i] = -1; } var li = new List(); li.Add(1); while (li.Count() != 0) { int bit = Convert.ToString(li[0], 2).Where(x => x == '1').Count(); if (li[0] - bit >= 1) { if (m[li[0] - bit] > m[li[0]] + 1 || m[li[0] - bit] == -1) { li.Add(li[0] - bit); m[li[0] - bit] = m[li[0]] + 1; } } if (li[0] + bit <= N) { if (m[li[0] + bit] > m[li[0] + 1] || m[li[0] + bit] == -1) { li.Add(li[0] + bit); m[li[0] + bit] = m[li[0]] + 1; } } li.RemoveAt(0); } Console.WriteLine(m[N]); } //------------------------------------------------------------- static int[] ConvertStringArrayToIntArray(string[] array) { return Array.ConvertAll(array, str => int.Parse(str)); } static List ConvertStringArrayToIntList(string[] str) { var list = new List(); foreach (var c in str) list.Add(int.Parse(c)); return list; } } }