using System.Linq; using System.Collections.Generic; using System; public class P { public int c { get; set; } public int a { get; set; } } public class Hello { public static void Main() { var n = int.Parse(Console.ReadLine().Trim()); var ans = goBfs(n); Console.WriteLine(ans); } public static int goBfs(int n) { var d = new int[] { 1, -1 }; var q = new Queue
(); var used = new bool[n + 1]; q.Enqueue(new P { a = 1, c = 1 }); while (q.Count() > 0) { var w = q.Dequeue(); if (w.a == n) return w.c; if (used[w.a]) continue; used[w.a] = true; for (int i = 0; i < 2; i++) { var na = w.a + d[i] * bitcount(w.a); if (na >= 1 && na <= n && !used[na]) q.Enqueue(new P { a = na, c = w.c + 1 }); } } return -1; } public static int bitcount(int bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff); } }