"""
            a
+          -------
|    b-a /       /    f(a, b) = 2*a + f(a, b-a)
|       /       /     answer = N + f(X, N-X)
b       -------       COMD(a < b)
|     /  \    /
|    /    \  /
|   /      \/
+   -------

"""
#--- define function ---#
def calc(a, b):
    if a > b:
        a, b = b, a

    if a == b:
        return a
    else:
        return 2 * a + calc(a, b - a)


 #--- main ---#
N, X = input().split()
N, X = int(N), int(X)

ans = N + calc(X, N - X)
print(ans)
