n,x=map(int,input().split())
ans=0
if x<n/2:
    a=x
    b=n-x
    ans+=n-x
    while True:
        if b%a==0:
            ans+=a*2*(b//a)
            break
        else:
            ans+=a*2*(b//a)
            a,b=b%a,a
elif x==n/2:
    ans=3*x
elif x>n/2:
    a=n-x
    b=x
    ans+=x
    while True:
        if b%a==0:
            ans+=a*2*(b//a)
            break
        else:
            ans+=a*2*(b//a)
            a,b=b%a,a

print(ans)