using System; using System.Collections.Generic; namespace No3_1{ public class Program{ private static List stack = new List(); private static int min = int.MaxValue; public static void Main(string[] args){ var result = Function(int.Parse(Console.ReadLine()), 1, 1); if(result == int.MaxValue) result = -1; Console.WriteLine(result); Console.ReadLine(); } public static int Function(int input, int place, int cnt){ if(place == input) return min = cnt; if(cnt >= input || cnt >= min || stack.Contains(place) || place < 1 || input < place) return int.MaxValue; stack.Add(place); var result = Math.Min(Function(input, place + BitOne(place), cnt + 1), Function(input, place - BitOne(place), cnt + 1)); return result; } public static int BitOne(int num){ var result = 0; while(true){ if(num % 2 == 1) result++; if((num /= 2) == 0) break; } return result; } } }