<?php
list($N,$X) = explode(" ",trim(fgets(STDIN)));
$x = min($X,$N - $X);
if($N % 2 == 0){
    if($N == $x * 2){
        echo $N + $x ."\n";
    }else{
        $n = 6 * ($N /2 - 1);
        if($x % 2 == 0){
            echo $n ."\n";
        }else{
            echo $n + 3 ."\n";
        }
    }
}else{
    if($N % $x != 0){
        $n = 3 * ($N - 1);
        echo $n ."\n";
    }else{
        $n = $x * 3 * ($N / $x - 1);
        echo $n ."\n";
    }
}
?>