S=input()
A=S.split()
N=int(A[0])
X=int(A[1])

Suma=0
Suma=Suma+X
Bx=X
By=N-X
Suma=Suma+By

while (Bx%By!=0):
    Path=int(Bx/By)
    Suma=Suma+By*(2*Path)
    Res=Bx%By
    Suma=Suma+2*Res
    Bx=Res
    By=By-Res
    
Suma=Suma+By*(2*Bx/By-1)
print(int(Suma))
