import sys

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

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

#print(sys.argv)


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

while rect_temp[0] > 0 and  rect_temp[1] > 0 :

    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]  ]



print(length)