n, x = map(int, input().split())

def func(a, b):
    res = 0
    while b > 0:
        c, d = divmod(a, b)
        res += 2 * b * c
        a = b
        b = d
    
    return res - a

if n == 2 * x:
    print(3 * x)
elif n > 2 * x:
    print(n + 2 * x + func(max(x, n-2*x), min(x, n-2*x)))
else:
    print(n + func(x, n-x))