using System; using System.Collections.Generic; using System.Linq; class P { static void DepthFirstSearch(int goal) { int x = path[path.Count - 1]; int step = Convert.ToString(x, 2).Replace("0", "").Length; if (x == goal) { result.Add(path.Count); } else { int post = x + step; if (!path.Contains(post) && 1 <= post && post <= goal) { path.Add(post); DepthFirstSearch(goal); } int pre = x - step; if (!path.Contains(pre) && 1 <= pre && pre <= goal) { path.Add(pre); DepthFirstSearch(goal); } } } static List path = new List() { 1 }; static List result = new List(); static void Main() { DepthFirstSearch(int.Parse(Console.ReadLine())); Console.WriteLine(result.Count == 0 ? -1 : result.Min()); } }