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



def func(a, b):

    if a < b:

        if b%a == 0:

            return (b//a*2 - 1)*a

        else:

            q, mod = divmod(b,a)

            return 2*q*a + func(a, mod)

    elif a > b:

        if a%b == 0:

            return (a//b*2 - 1)*b

        else:

            q, mod = divmod(a,b)

            return 2*q*b + func(mod, b)

    else:

        return a



if x == n/2:

    print((3*x))

else:

    print((x+(n-x)+func(x,n-x)))
# 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)))

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


def func(a, b):
    if a < b:
        if b % a == 0:
            return (b // a * 2 - 1) * a
        else:
            q, mod = divmod(b, a)
            return 2 * q * a + func(a, mod)
    elif a > b:
        if a % b == 0:
            return (a // b * 2 - 1) * b
        else:
            q, mod = divmod(a, b)
            return 2 * q * b + func(mod, b)
    else:
        return a


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

# 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)))

-n, x = list(map(int, input().split())) +# coding:utf-8 +def inpl(): +    return list(map(int, input().split())) + + +n, x = inpl() -        if b % a == 0: -            return (b // a * 2 - 1) * a -        else: -            q, mod = divmod(b, a) -            return 2 * q * a + func(a, mod) -    elif a > b: +        a, b = b, a +    if a == b: +        return a +    else: -            return 2 * q * b + func(mod, b) -    else: -        return a +            return 2 * q * b + func(b, mod) +            # return 2*b + func(a-b, b)#再帰回数が10000を超えるとエラー(20000000001, 4)など -    print((x + (n - x) + func(x, n - x))) +    print((n + func(x, n - x)))