/**
* Explaination : For both parts, I simulated results up to 1000
* and found an equation that could be used.
*/
$input = $input->int;
// ==================================================
// > PART 1
// ==================================================
$solution_1 = ($input - (2 ** (strlen(decbin($input)) - 1))) * 2 + 1;
// ==================================================
// > PART 2
// ==================================================
$n = $s = 2;
while (($n = ($n * 3 - 2)) <= $input) $s = $n;
$n = $input - $s + 1;
$solution_2 = $n <= $s ? $n : 2 * $n - $s + 1;