N,X=map(int,input().split())
A=N

X,N = sorted((X,N-X))
while N != X and X != 0:
    if N%X == 0:
        A += 2*(N//X-1)*X
        X,N=X,X
    else:
        A += 2*(N//X)*X
        X,N=sorted((X,N%X))
print(A+X)