Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)


Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from numba import jit

sN = []
sQ = []
Q = 0
N = 0


@jit
def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]['t']:
            if sQ[i]['d'] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


@jit
def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


@jit
def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]['t']:
            if sQ[i]['d'] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


@jit
def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)


Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]['t']:
            if sQ[i]['d'] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]['t']:
            if sQ[i]['d'] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)


Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    # init char list
    charList = [chr(s) for s in range(65, 91)] # A-Z

    N, Q = map(int, input().split()) # N, Q for number

    sN = list(input())  # squares
    sList = [None] * 26
    gList = [i for i in range(0,N)]  # golems
    for i in range(0, len(sN)): # init
        if(sList[charList.index(sN[i])] == None):
            sList[charList.index(sN[i])] = [{'id':i, 'loc':i}]
        else:
            sList[charList.index(sN[i])].append({'id':i, 'loc':i})

    sQ = []

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    remain = len(sN)
    for q in sQ: # loop to handle Q
        for golem in sList[charList.index(q['t'])]:
            if(q['d'] == 'L'):
                if(golem['loc'] == 0):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif(golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] - 1
                    sList[charList.index(sN[golem['loc']])].append({'id': golem['id'],'loc': golem['loc']})
            elif(q['d'] == 'R'):
                if(golem['loc'] == remain - 1):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif (golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] + 1
                    sList[charList.index(sN[golem['loc']])].append({'id': golem['id'],'loc': golem['loc']})

    print(remain)



Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    # init char list
    charList = [chr(s) for s in range(65, 91)] # A-Z

    N, Q = map(int, input().split()) # N, Q for number

    sN = list(input())  # squares
    sList = [None] * 26
    gList = [i for i in range(0,N)]  # golems
    for i in range(0, len(sN)): # init
        if(sList[charList.index(sN[i])] == None):
            sList[charList.index(sN[i])] = [{'id':i, 'loc':i}]
        else:
            sList[charList.index(sN[i])].append({'id':i, 'loc':i})

    sQ = []

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    remain = len(sN)
    for q in sQ: # loop to handle Q
        for golem in sList[charList.index(q['t'])]:
            if(q['d'] == 'L'):
                if(golem['loc'] == 0):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif(golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] - 1
                    sList[sN.index(golem['loc'])].append({'id': golem['id'],'loc': golem['loc']})
            elif(q['d'] == 'R'):
                if(golem['loc'] == remain - 1):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif (golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] + 1
                    sList[sN.index(golem['loc'])].append({'id': golem['id'],'loc': golem['loc']})

    print(remain)



Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    N, Q = map(int, input().split()) # N, Q for number
    sN = list(input())
    golems = []
    for i in range(0, N): # init golem
        golems.append({'loc': i, 'location':sN[i]})

    sQ = []

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    remain = len(golems)
    for q in sQ: # loop to handle Q
        for g in golems:
            if(g['loc'] >= 0 and g['location'] == q['t']):
                if(q['d'] == 'L'):
                    if(g['loc'] > 0):
                        g['loc'] = g['loc'] - 1
                        g['location'] = sN[g['loc']]
                    else:
                        g['loc'] = -1
                        remain = remain - 1
                elif(q['d'] == 'R'):
                    if(g['loc'] < N -1):
                        g['loc'] = g['loc'] + 1
                        g['location'] = sN[g['loc']]
                    else:
                        g['loc'] = -1
                        remain = remain - 1

    print(remain)

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    N, Q = map(int, input().split()) # N, Q for number
    sN = list(input())
    golems = []
    for i in range(0,N): # init golem
        golems.append({'loc': i})

    sQ = []
    remain = len(golems)
    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})
    for q in sQ: # loop to handle Q
        for g in golems:
            if(g['loc'] >= 0 and sN[g['loc']] == q['t']):
                if(q['d'] == 'L'):
                    if(g['loc'] <= 0):
                        g['loc'] = -1
                        remain = remain - 1
                    else:
                        g['loc'] = g['loc'] - 1
                elif(q['d'] == 'R'):
                    if(g['loc'] >= N -1):
                        g['loc'] = -1
                        remain = remain - 1
                    else:
                        g['loc'] = g['loc'] + 1

    print(remain)

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

def SearchIdx(s, c):
    idx = []
    for i in range(0,len(s)):
        if(s[i] == c):
            idx.append(i)
    return idx
if __name__ == '__main__':
    N, Q = map(int, input().split()) # N, Q for number
    sN = list(input())
    golems = []
    for i in range(0,N): # init golem
        golems.append({'id':i, 'loc': i, 'location': sN[i]})

    sQ = []
    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})
    for q in sQ: # loop to handle Q
        for g in golems:
            if(g['loc'] >= 0 and sN[g['loc']] == q['t']):
                if(q['d'] == 'L'):
                    if(g['loc'] <= 0):
                        g['loc'] = -1
                        g['location'] = ''
                    else:
                        g['loc'] = g['loc'] -1
                        g['location'] = sN[g['loc']]
                elif(q['d'] == 'R'):
                    if(g['loc'] >= N -1):
                        g['loc'] = -1
                        g['location'] = ''
                    else:
                        g['loc'] = g['loc'] + 1
                        g['location'] = sN[g['loc']]

    remain = len(golems)
    for g in golems:
        if(g['loc'] == -1):
            remain = remain - 1

    print(remain)

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)