Buggy python submission (verdict: Time Limit Exceeded):
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

mem = [0] * N
def isdead(xp):
    if mem[xp] == 1:
        return True
    elif mem[xp] == 2:
        return False
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0 or xp > N-1:
            mem[oxp] = 1
            return True
    mem[oxp] = 2
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

p1 = 0
p3 = N-1
p2 = mid(p1,p3)
ap = -1

for i in range(2,N):
    for j in range(0,N,int(N/i)):
        if not isdead(j):
            ap = j
            break
    if ap != -1:
        break
if ap == -1:
    print(0)
    exit()

#left
p1 = 0
p3 = ap
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdead(p1):
    p = p3
else:
    p = p1
#right
p1 = ap
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdead(p3):
    q = p1
else:
    q = p3


if p > q:
    print(0)
else:
    print(q-p+1)


Fixed submission:
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdeadleft(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            return True
        if xp > N-1:
            xp = N-1
    return False

def isdeadright(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            xp = 0
        if xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

if isdeadleft(N-1) or isdeadright(0):
    print(0)
    exit()

#left
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadleft(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdeadleft(p1):
    p = p3
else:
    p = p1
#right
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadright(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdeadright(p3):
    q = p1
else:
    q = p3

if p > q:
    print(0)
else:
    print(q-p+1)

Buggy python submission (verdict: Time Limit Exceeded):
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdead(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0 or xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

p1 = 0
p3 = N-1
p2 = mid(p1,p3)
ap = -1
#left
while p1 != p2:
    if isdead(p2):
        p2,p3 = int((p1+p2)/2),p2
    else:
        ap = p2
        break
#right
if ap == -1:
    while p2 != p3:
        if isdead(p2):
            p2,p1 = int((p2+p3)/2),p2
        else:
            ap = p2
            break
if ap == -1:
    print(0)
    exit()

#left
p1 = 0
p3 = ap
p2 = mid(p1,p3)
while p3 - p1 > 1:
    if isdead(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdead(p1):
    p = p3
else:
    p = p1
#right
p1 = ap
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 > 1:
    if isdead(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdead(p3):
    q = p1
else:
    q = p3


if p > q:
    print(0)
else:
    print(q-p+1)


Fixed submission:
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdeadleft(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            return True
        if xp > N-1:
            xp = N-1
    return False

def isdeadright(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            xp = 0
        if xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

if isdeadleft(N-1) or isdeadright(0):
    print(0)
    exit()

#left
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadleft(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdeadleft(p1):
    p = p3
else:
    p = p1
#right
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadright(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdeadright(p3):
    q = p1
else:
    q = p3

if p > q:
    print(0)
else:
    print(q-p+1)

Buggy python submission (verdict: Time Limit Exceeded):
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

mem = [0] * N
def isdead(xp):
    if mem[xp] == 1:
        return True
    elif mem[xp] == 2:
        return False
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0 or xp > N-1:
            mem[oxp] = 1
            return True
    mem[oxp] = 2
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

pf = 0
qf = N-1

p1 = 0
p3 = N-1
p2 = mid(p1,p3)
ap = -1
#left
while p1 != p2:
    if isdead(p2):
        p2,p3 = int((p1+p2)/2),p2
    else:
        pf = p1
        ap = p2
        break
#right
if ap == -1:
    while p2 != p3:
        if isdead(p2):
            p2,p1 = int((p2+p3)/2),p2
        else:
            ap = p2
            qf = p3
            break
if ap == -1:
    print(0)
    exit()

#left
p1 = pf
p3 = ap
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdead(p1):
    p = p3
else:
    p = p1
#right
p1 = ap
p3 = qf
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdead(p3):
    q = p1
else:
    q = p3


if p > q:
    print(0)
else:
    print(q-p+1)


Fixed submission:
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdeadleft(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            return True
        if xp > N-1:
            xp = N-1
    return False

def isdeadright(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            xp = 0
        if xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

if isdeadleft(N-1) or isdeadright(0):
    print(0)
    exit()

#left
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadleft(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdeadleft(p1):
    p = p3
else:
    p = p1
#right
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadright(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdeadright(p3):
    q = p1
else:
    q = p3

if p > q:
    print(0)
else:
    print(q-p+1)

Buggy python submission (verdict: Time Limit Exceeded):
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

mem = [0] * N
def isdead(xp):
    if mem[xp] == 1:
        return True
    elif mem[xp] == 2:
        return False
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0 or xp > N-1:
            mem[oxp] = 1
            return True
    mem[oxp] = 2
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

p1 = 0
p3 = N-1
p2 = int((p1+p3) / 2)
ap = -1
#left
while p1 != p2:
    if isdead(p2):
        p2,p3 = int((p1+p2)/2),p2
    else:
        ap = p2
        break
#right
if ap == -1:
    while p2 != p3:
        if isdead(p2):
            p2,p1 = int((p2+p3)/2),p2
        else:
            ap = p2
            break
if ap == -1:
    print(0)
    exit()

#left
p1 = 0
p3 = ap
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdead(p1):
    p = p3
else:
    p = p1
#right
p1 = ap
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdead(p3):
    q = p1
else:
    q = p3


if p > q:
    print(0)
else:
    print(q-p+1)


Fixed submission:
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdeadleft(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            return True
        if xp > N-1:
            xp = N-1
    return False

def isdeadright(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            xp = 0
        if xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

if isdeadleft(N-1) or isdeadright(0):
    print(0)
    exit()

#left
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadleft(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdeadleft(p1):
    p = p3
else:
    p = p1
#right
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadright(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdeadright(p3):
    q = p1
else:
    q = p3

if p > q:
    print(0)
else:
    print(q-p+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

mem = [0] * N
def isdead(xp):
    if mem[xp] == 1:
        return True
    elif mem[xp] == 2:
        return False
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0 or xp > N-1:
            mem[oxp] = 1
            return True
    mem[oxp] = 2
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

p1 = 0
p3 = N-1
p2 = int((p1+p3) / 2)
ap = -1
#left
while p1 != p2:
    if isdead(p2):
        p2,p3 = int((p1+p2)/2),p2
    else:
        ap = p2
        break
#right
if ap == -1:
    while p2 != p3:
        if isdead(p2):
            p2,p1 = int((p2+p3)/2),p2
        else:
            ap = p2
            break
if ap == -1:
    print(0)
    exit()

#left
p1 = 0
p3 = ap
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdead(p1):
    p = p3
else:
    p = p1
#right
p1 = ap
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdead(p3):
    q = p1
else:
    q = p3


if p > q:
    print(0)
else:
    print(q-p+1)


Fixed submission:
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdeadleft(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            return True
        if xp > N-1:
            xp = N-1
    return False

def isdeadright(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            xp = 0
        if xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

if isdeadleft(N-1) or isdeadright(0):
    print(0)
    exit()

#left
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadleft(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdeadleft(p1):
    p = p3
else:
    p = p1
#right
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadright(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdeadright(p3):
    q = p1
else:
    q = p3

if p > q:
    print(0)
else:
    print(q-p+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdead(xp):
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0 or xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

p1 = 0
p3 = N-1
p2 = int((p1+p3) / 2)
ap = -1
#left
while p1 != p2:
    if isdead(p2):
        p2,p3 = int((p1+p2)/2),p2
    else:
        ap = p2
        break
#right
if ap == -1:
    while p2 != p3:
        if isdead(p2):
            p2,p1 = int((p2+p3)/2),p2
        else:
            ap = p2
            break
if ap == -1:
    print(0)
    exit()

#left
p1 = 0
p3 = ap
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdead(p1):
    p = p3
else:
    p = p1
#right
p1 = ap
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdead(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdead(p3):
    q = p1
else:
    q = p3


if p > q:
    print(0)
else:
    print(q-p+1)


Fixed submission:
input = raw_input

N,Q = map(int,input().split(" "))
s=list(input())
ju = []
for i in range(Q):
    ju.append(input().split(" "))

def isdeadleft(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            return True
        if xp > N-1:
            xp = N-1
    return False

def isdeadright(xp):
    oxp = xp
    for i in range(Q):
        com = ju[i]
        if s[xp] == com[0]:
            if com[1] == "L":
                xp -= 1
            else:
                xp += 1
        if xp < 0:
            xp = 0
        if xp > N-1:
            return True
    return False

def mid(x1,x2):
    return int((x1+x2)/2)

if isdeadleft(N-1) or isdeadright(0):
    print(0)
    exit()

#left
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadleft(p2):
        p1,p2 = p2,mid(p2,p3)
    else:
        p2,p3 = mid(p1,p2),p2
if isdeadleft(p1):
    p = p3
else:
    p = p1
#right
p1 = 0
p3 = N-1
p2 = mid(p1,p3)
while p3 - p1 != 1:
    if isdeadright(p2):
        p2,p3 = mid(p1,p2),p2
    else:
        p1,p2 = p2,mid(p2,p3)
if isdeadright(p3):
    q = p1
else:
    q = p3

if p > q:
    print(0)
else:
    print(q-p+1)