import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import core.bitop; struct Block { uint bits; bool forward, backward; uint optimum = uint.max; } void main() { immutable n = readln.chomp.to!uint; auto game = new Block[n + 1]; foreach (i, ref x; game) x.bits = popcnt(cast(uint)i); uint[] route = [1]; do { auto point = route.back; if (point < 1 || point > n) { route.popBack; continue; } with (game[point]) { if (optimum > route.length) { optimum = cast(uint)route.length; forward = backward = false; } if (optimum < route.length) { route.popBack; continue; } if (!forward) { route ~= point + bits; forward = true; } else if (!backward) { route ~= point - bits; backward = true; } else { route.popBack; } } } while (!route.empty); writeln(cast(int)game[n].optimum); }