n, x = map(int, raw_input().split())
s = n
r = 1
if n - x > x:
    a, b = n - x ,x
else:
    a, b = x, n - x
while r > 0:
    q = a / b
    r = a % b
    if r > 0:
        s += 2 * q * b
    else:
        s += (2 * q - 1) * b
    a, b = b, r
print s