n, x = map(int,input().split())

if n % 2 == 0 and n//2 == x:
    print(3 * x)
    exit()

if x > n // 2:
    x = n - x

xx = x
yy = n - x

ans = n
i = 0
f = False

while True:
    if yy >= xx:
        ans += 2 * xx
        yy -= xx

        if yy == 0:
            ans -= xx
            f = True
            break

        if f:
            break

    if xx > yy:
        ans += yy * 2
        xx -= yy

        if xx == 0:
            ans -= yy
            f = True
            break

        if f:
            break

print(ans)
