Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]


def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False


if N==1:
	for x,y in q:
		if s==x:
			print 1
			quit()
	else:
		print 0


#左側から落ちるゴーレム
l=-1
r=N
while l!=r:
	m=(l+r)/2+1
	if leftout(m):
		l=m
	else:
		r=m-1
sl=l

#右側から落ちるゴーレム
l=-1
r=N
while l!=r:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m+1
sr=r


if sr>sl:
	print sr-sl-1
else:
	print N

Fixed submission:
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]

def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False


l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if leftout(m):
		l=m
	else:
		r=m
sl=l

l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m

sr=r


ans=N
if sl>=0:
	ans-=(sl+1)
if sr<N:
	ans-=(N-sr)

print ans

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]


def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False



#左側から落ちるゴーレム
l=-1
r=N
while l!=r:
	m=(l+r)/2+1
	if leftout(m):
		l=m
	else:
		r=m-1
sl=l


#右側から落ちるゴーレム
l=-1
r=N
while l!=r:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m+1
sr=r

print sr-sl-1


Fixed submission:
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]

def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False


l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if leftout(m):
		l=m
	else:
		r=m
sl=l

l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m

sr=r


ans=N
if sl>=0:
	ans-=(sl+1)
if sr<N:
	ans-=(N-sr)

print ans

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]


def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False



#左側から落ちるゴーレム
l=0
r=N
while l!=r:
	m=(l+r)/2+1
	if leftout(m):
		l=m
	else:
		r=m-1
sl=l


#右側から落ちるゴーレム
l=0
r=N
while l!=r:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m+1
sr=r

print sr-sl-1



Fixed submission:
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]

def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False


l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if leftout(m):
		l=m
	else:
		r=m
sl=l

l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m

sr=r


ans=N
if sl>=0:
	ans-=(sl+1)
if sr<N:
	ans-=(N-sr)

print ans

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]


def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
	else:
		return False



#左側から落ちるゴーレム
l=0
r=N
while l!=r:
	m=(l+r)/2+1
	if leftout(m):
		l=m
	else:
		r=m-1
sl=l


#右側から落ちるゴーレム
l=0
r=N
while l!=r:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m+1
sr=r

print sr-sl-1



Fixed submission:
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]

def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False


l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if leftout(m):
		l=m
	else:
		r=m
sl=l

l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m

sr=r


ans=N
if sl>=0:
	ans-=(sl+1)
if sr<N:
	ans-=(N-sr)

print ans

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]


M={}	#文字毎の場所のインデックス

for i,x in enumerate(s):
	if x not in M:
		M[x]=[i]
	else:
		M[x].append(i)

G=[ 1 for i in range(N) ]  #各マスのゴーレムの数

ans=0

for l,r in q:
	if l not in M: continue
	for x in M[l]:
		if r=="L":
			if x-1==-1:
				ans+=G[0]
				G[x]=0
			else:
				G[x-1]+=G[x]
				G[x]=0
		elif r=="R":
			if x+1==N:
				ans+=G[N-1]
				G[x]=0
			else:
				G[x+1]+=G[x]
				G[x]=0

print N-ans


Fixed submission:
# -*- coding: utf-8 -*-
import sys

N,Q=map(int, sys.stdin.readline().split())
s=raw_input()
q=[ sys.stdin.readline().split()  for i in range(Q) ]

def leftout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur<0:
			return True
			break
		elif cur>N-1:
			return False
			break			

	else:
		return False


def rightout(cur):
	for x,y in q:
		if s[cur]==x:
			if y=="L":
				cur-=1
			elif y=="R":
				cur+=1
		if cur>N-1:
			return True
			break
		if cur<0:
			return False
			break
	else:
		return False


l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if leftout(m):
		l=m
	else:
		r=m
sl=l

l=-1
r=N
while r-l!=1:
	m=(l+r)/2
	if rightout(m):
		r=m
	else:
		l=m

sr=r


ans=N
if sl>=0:
	ans-=(sl+1)
if sr<N:
	ans-=(N-sr)

print ans