using System.Numerics; public class Program { public static void Main() { //BigInteger num = BigInteger.Parse(Console.ReadLine() ?? string.Empty); int num = int.Parse(Console.ReadLine() ?? string.Empty); //string[] moji = (Console.ReadLine() ?? string.Empty).Trim().Split(' '); //string str = Console.ReadLine() ?? string.Empty; int count = 1; int math = 1; int numnum = math; for(int i = 0;i <= num;i++) { count++; string nn = Convert.ToString(math, 2); nn = nn.Replace("0", ""); if(math + nn.Length == num) { Console.WriteLine(count-1); return; } if(math + nn.Length > num) { if (math -nn.Length == numnum) { Console.WriteLine(-1); return; } numnum = math; math-=nn.Length; } else { numnum = math; math += nn.Length; } } Console.WriteLine(-1); //while(math < num) //{ // count++; // string nn=Convert.ToString(math,2); // nn = nn.Replace("0", ""); // math += nn.Length; // if(math == num) // { // Console.WriteLine(count); // return; // } // if(math > num) // { // Console.WriteLine(-1); // } //} //下がるとき1回来たマスに戻る場合は-1出力 } }