value = input()
values = value.split()

N = int( values[0] )
X = int( values[1] )

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)