n, x = 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))