n, x = input().split()
n = int(n)
x = int(x)
ans = 0
a1, a2 = x, n - x
l = min(a1, a2)
h = max(a1, a2)
while True:
    d = h // l
    ans += 3 * l * d
    temp = h - l * d
    if temp == 0:
        break
    h = l
    l = temp
print(ans)