-- C
local DBG = true
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

-- https://golang.org/src/sort/search.go?s=2246:2286#L49
local function find_min_true(n, f)
    local function range_ok(x)
        return x >= 1 and x <= n
    end
    local function loop(i, j)
        -- Define f(0) == false, f(n+1) == true
        -- Invariant: f(i-1) == false and f(j) == true
        if i == j then
            assert(i >= 1 and i <= n + 1) -- i == n + 1 is valid
            -- i == j, f(i-1) == false, f(i) == true
            -- i is the minimum number that f(i) == true
            return i
        end
        local h = (i + j) // 2
        assert(h >= 1 and h <= n)
        if f(h) then
            -- f(h) == true
            -- j = h ==> f(j) == true
            return loop(i, h)
        else
            -- f(h) == false
            -- i = h + 1 ==> f(i-1) == false
            return loop(h + 1, j)
        end
    end
    -- f(i-1) == f(0) == false
    -- f(j) == f(n+1) == true
    return loop(1, n + 1)
end

local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

local function each_char(s)
    local f = function(_, i)
        i = i + 1
        if i <= #s then
            return i, string.sub(s, i, i)
        end
    end
    return f, nil, 0
end

local N, Q, _ = io.read("n", "n", "l")
local s = io.read("l")
assert(#s == N)
local spells = {}
for i=1,Q do
    local t, _, d, _ = io.read(1, 1, 1, "l")
    spells[i] = {t, d}
end

local place = str2tbl(s)

local function simulation_leftdeath(pos)
    for i=1,Q do
        local t, d = spells[i][1], spells[i][2]
        if place[pos] == t then
            if d == 'L' then
                pos = pos - 1
            else
                pos = pos + 1
            end
            if pos == 0 then
                return true
            end
            if pos == N + 1 then
                return false
            end
        end
    end
    return false
end
local function simulation_rightdeath(pos)
    for i=1,Q do
        local t, d = spells[i][1], spells[i][2]
        if place[pos] == t then
            if d == 'L' then
                pos = pos - 1
            else
                pos = pos + 1
            end
            if pos == 0 then
                return false
            end
            if pos == N + 1 then
                return true
            end
        end
    end
    return false
end

local rightmost_dead_of_left = -1 + find_min_true(N, function(x)
    return simulation_leftdeath(x) == false
end)

local leftmost_dead_of_right = find_min_true(N, function(x)
    return simulation_rightdeath(x) == true
end)

local dead = 0
for i=1,N do
    if i <= rightmost_dead_of_left or i >= leftmost_dead_of_right then
        dead = dead + 1
    end
end
print(N - dead)

Measured memory_bytes: 34740