#!/usr/bin/perl
use Math::BigInt;

while (<>) {
    ($n, $x) = split /\s/, $_;
    last;
}

$d = Math::BigInt->new($n);

if ($x <= $n / 3) {
    $d =  ($n - $x) * 3;
} elsif ($n / 3 < $x && $x < $n / 2) {
    $d = ($n - $x) * 4;
} elsif ($n / 2 == $x) {
    $d =  $x * 3;
} elsif ($n / 2 < $x && $x < ($n * 2) / 3) {
    $d = ($n - $x) * 2 * 3;
} else {
    $d = $x * 3;
}

print "$d\n";
