n, x = map((x) -> parse(Int, x), split(readline(stdin), ' '))

function a(longline, shortline)
    if shortline == 0
        return 0
    else
        return longline+shortline+a(shortline, longline-shortline)
end
println(a(n-x, x))