def is_prime(n: int)->bool:
    d = 2
    while d * d <= n:
        if n % d == 0:
            return False
        d += 1
    return True


def mysterious_light(N: int, X: int)->int:
    if is_prime(N):
        return 3*(N-1)
    return 3*max(N-X, X)


if __name__ == "__main__":
    N, X = map(int, input().split())

    ans = mysterious_light(N, X)
    print(ans)
