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