using System; using System.Collections.Generic; using System.Linq; class P { static int Search(int goal) { Random r = new Random((int)DateTime.Now.Ticks); int x = 1; int c = 1; int step = 1; KeyValuePair post; KeyValuePair pre; path.Add(new KeyValuePair(x, c)); minX[x] = Math.Min(1, c); memo[x][1] = true; if (x == goal) { return 1; } while (path.Count != 0) { if (x == goal) { return c; } else { KeyValuePair p = path.First(); x = p.Key; c = p.Value; path.Remove(path.First()); step = Convert.ToString(x, 2).Replace("0", "").Length; post = new KeyValuePair(x + step, c + 1); pre = new KeyValuePair(x - step, c + 1); if (1 <= post.Key && post.Key <= goal) { if (!path.Contains(post)) { if (minX[post.Key] == -1) { minX[post.Key] = post.Value; } else { minX[post.Key] = Math.Min(minX[post.Key], post.Value); } if (!memo[x][post.Key]) { path.Add(new KeyValuePair(post.Key, minX[post.Key])); memo[x][post.Key] = true; } } } if (1 <= pre.Key && pre.Key <= goal) { if (!path.Contains(pre)) { if (minX[pre.Key] == -1) { minX[pre.Key] = pre.Value; } else { minX[pre.Key] = Math.Min(minX[pre.Key], pre.Value); } if (!memo[x][pre.Key]) { path.Add(new KeyValuePair(pre.Key, minX[pre.Key])); memo[x][pre.Key] = true; } } } } } return -1; } static bool[][] memo; static int[] minX; static List> path = new List>(); static void Main() { int n = int.Parse(Console.ReadLine()); memo = new bool[n+1][]; for (int i = 0; i < memo.Length; i++) { memo[i] = Enumerable.Range(0, n+1).Select(a => false).ToArray(); } minX = Enumerable.Range(0, n+1).Select(a => -1).ToArray(); Console.WriteLine(Search(n)); } }