n, x = map(int, input().split())
def f(a, b):
    if (a > b):
        a, b = b, a
    if b % a == 0:
        return (b // a * 2 - 1) * a
    return b // a * 2 * a + f(b % a, a)

print(n + f(x, n - x))
