import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

def F(A,B):
    # (A,B)の平行四辺形の120度の対角から出た状態
    if A > B:
        A,B = B,A
    q,r = divmod(B,A)
    if not r:
        return A * (q+q-1)
    return F(A,r) + q * (2 * A)

answer = N + F(X,N-X)
print(answer)