using System; using System.Collections.Generic; using System.Linq; class P { static int Search(int goal) { int x = path.Last(); int step = Convert.ToString(x, 2).Replace("0", "").Length; int post = x + step; int pre = x - step; int count = 0; path.Add(post); if (x == goal) { return 1; } while (path.Count != 0 && count != path.Count) { if (x == goal) { return path.Count + 1; } else { x = path.Last(); step = Convert.ToString(x, 2).Replace("0", "").Length; post = x + step; pre = x - step; if (!path.Contains(post) && 1 <= post && post <= goal) { path.Add(post); } else if (!path.Contains(pre) && 1 <= pre && pre <= goal) { path.Add(pre); } else { count = path.Count; path.Remove(x); } } } return -1; } static List path = new List() { 1 }; static void Main() { Console.WriteLine(Search(int.Parse(Console.ReadLine()))); } }