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 = -1; } void main() { immutable n = readln.chomp.to!uint; auto game = new Block[n + 1]; foreach (i, ref x; game) x.bits = i.popcnt; uint[] route = [1]; while (!route.empty) { auto point = route.back; if (point < 1 || point > n) { route.popBack; continue; } with (game[point]) { optimum = min(route.length, optimum); if (!forward) { route ~= point + bits; forward = true; } else if (!backward) { route ~= point - bits; backward = true; } else { route.popBack; } } } writeln(cast(int)game[n].optimum); }