n,x=map(int,input().split())
c=n
def sol(a,b):
    global c
    if a==b:c+=a
    elif a%b==0:c+=b*(2*(a//b)-1)
    elif b%a==0:c+=a*(2*(b//a)-1)
    elif a>b:
        c+=b*(a//b)*2
        sol(a-a//b*b,b)
    else:
        c+=a*(b//a)*2
        sol(a,b-b//a*a)
sol(x,n-x)
print(c)