N,X = map(int, input().split())

if N/2 == X:
    l = X*3
elif N/2 > X:
    l = N
    x = X
    n = N - X
    while x > 0:
        l += (n//x)*2*x
        c = x
        x = n%x
        n = c
    l -= n
else:
    l = N
    x = X
    n = N - X
    while x > 0:
        l += (n//x)*2*x
        c = x
        x = n%x
        n = c
    l -= n
print(l)