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

def f(a, b):
    if not a * b:
        return 0
    if not (a - b):
        return a
    return f(min(a, b), max(a, b) % min(a, b)) + 2 * min(a, b) * (max(a,b)//min(a, b))

res = 2 * x + f(n - x, x)

print(res)
