n, x = map(int, raw_input().split())


def solve(a, b):
    if a > b:return solve(b, a)
    if b % a == 0:
        return (2 * (b / a) - 1) * a
    return 2 * a * (b / a) + solve(a, b % a)

print solve(x, n-x) + n
