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


def f(a, b):
    if a == b:
        return a
    if a > b:
        a, b = b, a
    if b % a == 0:
        return 2 * b - a
    else:
        div, mod = divmod(b, a)
        return 2 * a * div + f(a, mod)

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