N, X = map(int, input().split())
hei = [X, N - X]
distance = N

while hei[0] != hei[1]:
    if hei[0] > hei[1]:
        distance += 2 * hei[1]
        hei[0] = hei[0]- hei[1]
    else:
        distance += 2 * hei[0]
        hei[1] = hei[1] - hei[0]

distance += hei[0]

print(distance)