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


def para(a,b):
    if a == b:
        return a
    else:
        if a%b == 0:
            return (2*(a//b) -1)*b
        if a%b != 0:
            return  (2*(a//b) -1)*b + para(b,a%b)
if X >= N-X:
     print(N+para(X,N-X))
if X <= N-X:
     print(N+para(N-X,X))