using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace YukiCoderCS { class Program { public static void Main(string[] args) => new Program().Solve(); int bitCount(int x) { x = x - ((x >> 1) & 0x55555555); x = (x & 0x33333333) + ((x >> 2) & 0x33333333); x = (x + (x >> 4)) & 0x0f0f0f0f; x = x + (x >> 8); x = x + (x >> 16); return x & 0x3f; } void Solve() { var N = int.Parse(Console.ReadLine()); var minCosts = new int[N + 1]; var start = 1; var que = new Queue(); var ans = -1; minCosts[start] = 1; que.Enqueue(start); while (que.Count > 0) { var x = que.Dequeue(); if (x == N) { ans = minCosts[x]; break; } var bit = bitCount(x); var f = x - bit; var b = x + bit; if (f >= 1 && minCosts[f] == 0) { que.Enqueue(f); minCosts[f] = minCosts[x] + 1; } if (b <= N && minCosts[b] == 0) { que.Enqueue(b); minCosts[b] = minCosts[x] + 1; } } Console.WriteLine(ans); } } }