#!/usr/bin/perl

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

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