using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int num = int.Parse(Reader.ReadLine()); int[] map = new int[num + 1]; map[1] = 1; Queue q = new Queue(); q.Enqueue(1); while(q.Count()>0) { int cur = q.Dequeue(); int step = map[cur]; int move = Convert.ToString(cur, 2).Count(a => a == '1'); int[] nextPos = new int[] { cur - move, cur + move }; foreach(int next in nextPos) { if(next<=1) { continue; } if(next>num) { continue; } if(map[next]==0||map[next]>step+1) { map[next] = step + 1; q.Enqueue(next); } } } if(map[num]==0) { Console.WriteLine(-1); } else { Console.WriteLine(map[num]); } } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 5 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }