def solve(x, y):
    a = max(x, y)
    b = min(x, y)
    if b == 0:
        return 0
    res = 2*a
    r = solve(b-a % b, a % b)
    if r == 0:
        res -= b
    res += r
    return res


s = raw_input().split()
n = int(s[0])
x = int(s[1])

ans = n + solve(x, n-x)
print(ans)
