import std.algorithm, std.array, std.range; import std.string, std.conv; import std.math; import std.stdio, std.typecons; alias Tuple!(int, "p", int, "c") qitem; void main() { auto n = readln.chomp.to!int; writeln(calc(n)); } int calc(int n) { auto q = [qitem(1, 1)]; auto visited = new bool[n + 1]; while (!q.empty) { auto qi = q.front; q.popFront; visited[qi.p] = true; if (qi.p == n) { return qi.c; } else { auto b = bits(qi.p); auto p1 = qi.p - b; if (p1 >= 1 && !visited[p1]) q ~= qitem(p1, qi.c + 1); auto p2 = qi.p + b; if (p2 <= n && !visited[p2]) q ~= qitem(p2, qi.c + 1); } } return -1; } int bits(int n) { auto c = 0; while (n > 0) { if ((n & 1) == 1) c += 1; n >>= 1; } return c; }