import sys

N = int(sys.argv[1])
X = int(sys.argv[2])


rect = [ X, N-X ]
rect_temp = rect
length = 0

while True :

    if rect_temp[0] < rect_temp[1]:
        length += rect_temp[0]*3
        rect_temp = [ rect_temp[0], rect_temp[1]-rect_temp[0]  ]

    elif rect_temp[0] > rect_temp[1]:
        length += rect_temp[1]*3
        rect_temp = [ rect_temp[0] - rect_temp[1], rect_temp[0]  ]

    elif rect_temp[0] == rect_temp[1]:
        break


print(length)