using System; using System.Collections.Generic; using System.Linq; namespace No003_ビットすごろく { class Program { static private int Min; static private int N; static void Main(string[] args) { N = int.Parse(Console.ReadLine()); Min = N; bool[] progression = Enumerable.Repeat(false, N + 1).ToArray(); Search(1, 1, progression); if (Min == N) Console.WriteLine(-1); else Console.WriteLine(Min); } static void Search(int level, int count, bool[] progressionB) { bool[] progression = new bool[progressionB.Count()]; Array.Copy(progressionB, progression, progressionB.Count()); if (level == N) { // Console.WriteLine("★★★ " + level.ToString() + " " + BitNumber(level).ToString()+" "+count.ToString()); if (Min > count) Min = count; } else if (Min < count) { return; } else if (level > 0 && level <= N && !progression[level]) { // Console.WriteLine(level.ToString() + " " + BitNumber(level).ToString() + " " + count.ToString()); progression[level] = true; Search(level + BitNumber(level), count + 1, progression); Search(level - BitNumber(level), count + 1, progression); } } static int BitNumber(int a) { int temp = 0; do { temp += a % 2; a /= 2; } while (a > 0); return temp; } } }