# coding:utf-8


def inpl(): return list(map(int, input().split()))


n, x = inpl()


def func(a, b):
    if a < b:
        a, b = b, a
    if a == b:
        return a
    else:
        if a % b == 0:
            return (a // b * 2 - 1) * b
        else:
            q, mod = divmod(a, b)
            return 2 * q * b + func(b, mod)
            # return 2*b + func(a-b, b)#再帰回数が10000を超えるとエラー(20000000001, 4)など


if x == n / 2:
    print(3 * x)
else:
    print(n + func(x, n - x))
