(N, X) = tuple([int(term) for term in input().split()])

T = N

A = N - X
B = X
while (True) :
    if (B == 0) :
        break
    n = int(A / B)
    if (A % B == 0) :
        T += (2 * n - 1) * B
        break
    T += 2 * n * B + 2 * (A % B)
    a = A % B
    b = B - (A % B)
    A = a
    B = b

print(T)