"""取込"""
n, x = [int(i) for i in input().split(" ")]

"""問題"""
def f(a, b):
    # print("({0}, {1})".format(a, b))
    l = max(a, b)
    s = min(a, b)
    if s == 0:
        return -l
    else:
        return (l // s) * s * 2 + f(l % s, s)

d = n + f(n - x, x)

"""出力"""
print(d)
