using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); List queue = new List(); List al = new List(); List visit = new List(); int R = 1; al.Add(R); queue.Add(R); visit.Add(R); while (queue.Contains(R) && (R != N || al.Count != 0)) { bool exist = false; string strR = Convert.ToString(R, 2); char[] cR = new char[strR.Length]; for (int i = 0; i < strR.Length; i++) { cR[i] = strR[i]; } int count = 0; for (int i = 0; i < cR.Length; i++) { if (cR[i].ToString() == "1") { count++; } } int[] node = new int[2]; node[0] = R - count; node[1] = R + count; foreach (int cnode in node) { if (1 <= cnode && cnode <= N && !visit.Contains(cnode)) { al.Add(cnode); queue.Add(cnode); visit.Add(cnode); exist = true; } } if (exist == false) { queue.Remove(R); } al.Remove(R); if (al.Count != 0) { R = al[0]; } } if (R == N) { queue.Add(R); Console.WriteLine(queue.Count); } else { Console.WriteLine(-1); } } } }