using System; using System.Linq; using System.Collections.Generic; namespace No003_ビットすごろく { class Program { static private int N; static private int M; static private int[] Bit; static private List Count = new List(); static void Main(string[] args) { N = int.Parse(Console.ReadLine()); BitCheck(); bool[] CheckerF = Enumerable.Repeat(false, M).ToArray(); bool[] CheckerB = Enumerable.Repeat(false, M).ToArray(); Recursive(1, 1, CheckerF, CheckerB); if (Count.Count == 0) Console.WriteLine(-1); else Console.WriteLine(Count.Min()); } static void BitCheck() { M = (int)Math.Pow(2.0, Math.Log((double)N, 2.0) + 1.0); int[] log2 = new int[M]; int temp, count; Bit = new int[M]; log2[0] = 1; log2[1] = 1; Bit[0] = 0; Bit[1] = 1; temp = 1; count = 0; for (int i = 2; i <= N; i++) { log2[i] = (int)Math.Log((double)i, 2.0) + 1; if (log2[i] != temp) { temp = log2[i]; count = 0; } Bit[i] = Bit[count] + 1; count++; } } static void Recursive(int level, int count, bool[] CheckerF, bool[] CheckerB) { if (level > M || level < 1) return; else if (level == N) { Count.Add(count); return; } if (!CheckerF[level]) { CheckerF[level] = true; Recursive(level + Bit[level], count + 1, CheckerF, CheckerB); } if (!CheckerB[level]) { CheckerB[level] = true; Recursive(level - Bit[level], count + 1, CheckerF, CheckerB); } return; } } }