using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] map = new int[N + 1]; map[1] = 1; Queue q = new Queue(); q.Enqueue(1); while (q.Count > 0) { int count = 0; int search = q.Dequeue(); int step = map[search]; string strR = Convert.ToString(search, 2); char[] cR = new char[strR.Length]; for (int i = 0; i < strR.Length; i++) { cR[i] = strR[i]; } for (int i = 0; i < cR.Length; i++) { if (cR[i].ToString() == "1") { count++; } } int[] node = new int[2]; node[0] = search - count; node[1] = search + count; foreach (int next in node) { if (1 >= next || next > N) { continue; } if (map[next] == 0 || map[next] > step + 1) { map[next] = step + 1; q.Enqueue(next); } } } if (map[N] == 0) { Console.WriteLine(-1); } else { Console.WriteLine(map[N]); } } } }