# import sys
# sys.setrecursionlimit(10 ** 9)
N, X = map(int, input().split())

# def f(a, b):
#     if a == b:
#         return a
#     if a >= b:
#         a, b = b, a
#     return 2 * a + f(a, b - a)

# print (N + f(N - X, X))




ans = N
a = N - X
b = X
while a != b:
    if a >= b:
        a, b = b, a
    ans += 2 * a
    b = b - a
print (ans + a)


    