<?php $n = trim(fgets(STDIN)); $map = array_fill(0, $n, -1); $queue[0] = $map[0] = $step = 1; while (!empty($queue)) { $pos = array_shift($queue); $step = $map[$pos -1 ]; $bit = substr_count(decbin($pos),1,0); if ($pos + $bit <= $n && -1 === $map[$pos + $bit -1]){ $move = $pos + $bit; $map[$move -1 ] = $step +1; $queue[] = $move; } if ($pos - $bit > 1 && -1 === $map[$pos - $bit -1]){ $move = $pos - $bit; $map[$move -1 ] = $step +1; $queue[] = $move; } } echo $map[$n -1 ] . PHP_EOL;