import math
N, X = list(map(int, input().split(' ')))
def f(a, b):
    a, b = min([a, b]), max([a, b])
    if b % a == 0:
        return 2 * a * math.floor(b / a) - a
    return 2 * a * math.floor(b / a) + f(a, b % a)
print(N + f(N - X, X))