using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace yukicoder { class Program { public static int N; static void Main(string[] args) { N = int.Parse(Console.ReadLine()); Console.WriteLine(Bfs(1)); } static int Bfs(int start) { Queue que = new Queue(); que.Enqueue(new int[] { start, 1 }); bool[] visited = new bool[N]; while(que.Count != 0) { int[] nums = que.Dequeue(); int currentNum = nums[0]; int count = nums[1]; if (currentNum == N) { return count; } if (visited[currentNum]) { continue; } else { visited[currentNum] = true; int next = numofbits5(currentNum); if(currentNum + next <= N) { que.Enqueue(new int[] { currentNum + next , ++count}); } if(currentNum - next >= 2) { que.Enqueue(new int[] { currentNum - next , ++count}); } } } return -1; } static int numofbits5(long 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 (int)((bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff)); } } }