You are given N squares numbered from 1 to N, each with a character written on it. Initially, there is one golem on each square. Snuke casts Q spells to move the golems. Each spell consists of a character and a direction (L or R). When a spell is cast, golems with the specified character move left or right. If a golem tries to move left from Square 1 or right from Square N, it disappears. Find the number of golems remaining after Snuke casts the Q spells. Constraints: 1 ≤ N,Q ≤ 2 × 10^5, |s| = N, s_i and t_i are uppercase English letters, and d_i is L or R. Input format: N Q, followed by s, t_i d_i for each spell. Output the number of remaining golems. Example: Input: 3 4 ABC A L B L B R A R Output: 2 (Explanation provided in the original problem description)

# Buggy version:
n,q=map(int,input().split())
s = input()
td = [input().split() for _ in range(q)]
def f(x):
  for t,d in td:
    if s[x] == t:
      if d == "L":
        x -= 1
      else:
        x += 1
      if 0 <= x < n:
        continue
      return x < 0
def g(x):
  for t,d in td:
    if s[x] == t:
      if d == "L":
        x -= 1
      else:
        x += 1
      if 0 <= x < n:
        continue
      return x >= n
ok = -1
ng = n
while ng - ok > 1:
  mid = (ok + ng)//2
  if f(mid):
    ok = mid
  else:
    ng = mid
ans = ok + 1
ok = n
ng = -1
while ok - ng > 1:
  mid = (ok + ng) //2
  if g(mid):
    ok = mid
  else:
    ng = mid
print(ans + (n - ok))

# Fixed version:
n,q=map(int,input().split())
s = input()
td = [input().split() for _ in range(q)]
def f(x):
  for t,d in td:
    if s[x] == t:
      if d == "L":
        x -= 1
      else:
        x += 1
      if 0 <= x < n:
        continue
      return x < 0
def g(x):
  for t,d in td:
    if s[x] == t:
      if d == "L":
        x -= 1
      else:
        x += 1
      if 0 <= x < n:
        continue
      return x >= n
ok = -1
ng = n
while ng - ok > 1:
  mid = (ok + ng)//2
  if f(mid):
    ok = mid
  else:
    ng = mid
ans = ok + 1
ok = n
ng = -1
while ok - ng > 1:
  mid = (ok + ng) //2
  if g(mid):
    ok = mid
  else:
    ng = mid
print(n - (ans + (n - ok)))

# Buggy version:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = map(str, input().split())
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = len(s)

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = len(s);
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = len(s);
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low
        
        

left_edge = check_left()
#print(left_edge)
right_edge = check_right()
#print(right_edge)

ans_s = s[left_edge+1:right_edge]
#print(ans_s)
ans = len(ans_s)
print(ans)

        
        
        
        
        
        

# Fixed version:
N, Q = map(int, input().split())
s = str(input())

t = []
d = []

for _ in range(Q):
    ti, di = input().split()
    t.append(ti)
    d.append(di)
    
LEFT_DIE = -1
RIGHT_DIE = N

def is_dead(i):
    for ti, di in zip(t, d):
        if s[i] == ti:
            if di == 'L':
                i -= 1
            if di == 'R':
                i += 1
            if i == LEFT_DIE:
                return LEFT_DIE
            if i == RIGHT_DIE:
                return RIGHT_DIE
    return 0

low = -1
high = N;
# LEFT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == LEFT_DIE:
        low = mid
    else:
        high = mid
left_edge = high

low = -1
high = N;
# RIGHT
while high - low > 1:
    mid = (low + high) // 2;
    if is_dead(mid) == RIGHT_DIE:
        high = mid
    else:
        low = mid
right_edge = low

#print(left_edge, right_edge)
ans_s = s[left_edge:right_edge+1]
#print(ans_s)
ans = len(ans_s)
print(ans)

# Buggy version:
n, q = map(int, input().split())
s = "_{}_".format(input())
td = [input().split() for _ in range(q)]

current_l = 0
current_r = n + 1

for t, d in reversed(td):

    if 0 < current_l + 1 < len(s) - 1 and t == s[current_l + 1] and d == "L":
        current_l += 1
    elif 0 < current_l - 1 < len(s) - 1 and t == s[current_l - 1] and d == "R":
        current_l -= 1

    if 0 < current_r + 1 < len(s) - 1 and t == s[current_r + 1] and d == "L":
        current_r += 1
    elif 0 < current_r - 1 < len(s) - 1 and t == s[current_r - 1] and d == "R":
        current_r -= 1

print(max(current_r - current_l - 1, 0))


# Fixed version:
n, q = map(int, input().split())
s = "_{}_".format(input())
td = [input().split() for _ in range(q)]

current_l = 0
current_r = len(s) - 1

for t, d in reversed(td):

    if current_l + 1 < len(s) - 1 and t == s[current_l + 1] and d == "L":
        current_l += 1
    elif t == s[current_l] and d == "R":
        current_l -= 1

    if 0 < current_r - 1 and t == s[current_r - 1] and d == "R":
        current_r -= 1
    elif t == s[current_r] and d == "L":
        current_r += 1

print(max(current_r - current_l - 1, 0))

# Buggy version:
def search_right(a, x, lo=0, hi=None, key=lambda x: x):
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < key(a[mid]): hi = mid
        else: lo = mid+1
    return lo


def search_left(a, x, lo=0, hi=None, key=lambda x: x):
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if key(a[mid]) < x: lo = mid+1
        else: hi = mid
    return lo


def get_fin_pos(s, td, golem_pos):
    print(golem_pos)
    for t, d in td:
        if 0 <= golem_pos < len(s) and s[golem_pos] == t:
            golem_pos += 1 if d == "R" else -1
    return golem_pos


n, q = map(int, input().split())
s = "_{}_".format(input())
spells = [input().split() for _ in range(q)]

left = search_right(range(len(s)), 0, key=lambda golem: get_fin_pos(s, spells, golem))
right = search_left(range(len(s)), len(s) - 1, key=lambda golem: get_fin_pos(s, spells, golem))

print(right - left)


# Fixed version:
def search_right(a, x, lo=0, hi=None, key=lambda x: x):
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < key(a[mid]): hi = mid
        else: lo = mid+1
    return lo


def search_left(a, x, lo=0, hi=None, key=lambda x: x):
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if key(a[mid]) < x: lo = mid+1
        else: hi = mid
    return lo


def get_fin_pos(s, td, golem_pos):
    for t, d in td:
        if 0 <= golem_pos < len(s) and s[golem_pos] == t:
            golem_pos += 1 if d == "R" else -1
    return golem_pos


n, q = map(int, input().split())
s = "_{}_".format(input())
spells = [input().split() for _ in range(q)]

left = search_right(range(len(s)), 0, key=lambda golem: get_fin_pos(s, spells, golem))
right = search_left(range(len(s)), len(s) - 1, key=lambda golem: get_fin_pos(s, spells, golem))

print(right - left)

# Buggy version:

def nibu():
    n,q=map(int,input().split())
    s=[0]+list(input())+[0]
    td=[list(map(str,input().split()))for i in range(q)][::-1]

    L=0
    R=n+1
    
    for t,d in td:
        if t==s[L+1] and d=='L':
            L+=1
        elif t==s[L] and d=='R':
            L-=1
        
        if t==s[R-1]==d=='R':
            R-=1
        elif t==s[R] and d=='L':
            R+=1

    return max(0,R-L-1)

print(nibu())

# Fixed version:
def nibu():
    n,q=map(int,input().split())
    s=[0]+list(input())+[0]
    td=[input().split()for i in range(q)][::-1]

    L=0
    R=n+1

    for t,d in td:
        if t==s[L+1] and d=='L':
            L+=1
        elif t==s[L] and d=='R':
            L-=1
        
        if t==s[R-1] and d=='R':
            R-=1
        elif t==s[R] and d=='L':
            R+=1

    return max(0,R-L-1)

print(nibu())

# Buggy version:
N, Q = map(int, input().split())
s = input()
TD = [input().split() for _ in range(Q)]

def check(x):
    #Q回の呪文後の位置をチェック
    #1 <= x <= N
    for t, d in TD:
        if s[x] == t:
            if d == 'L':
                x -= 1
            else:
                x += 1
        else:
            continue

        if x == 0:
            return -1
        if x == N + 1:
            return 1
    return 0

ok = 0
ng = N + 1
while ng - ok > 1:
    mid = (ok + ng) // 2
    if check(mid) == -1:
        ok = mid
    else:
        ng = mid
l = ok


ok = N + 1
ng = 0
while ok - ng > 1:
    mid = (ok + ng) // 2
    if check(mid) == 1:
        ok = mid
    else:
        ng = mid

r = ng

print(r - l)

# Fixed version:
N, Q = map(int, input().split())
s = '.' + input() + '.'
TD = [input().split() for _ in range(Q)]

def check(x):
    #Q回の呪文後の位置をチェック
    #1 <= x <= N
    for t, d in TD:
        if s[x] == t:
            if d == 'L':
                x -= 1
            else:
                x += 1
        if x == 0:
            return -1
        if x == N + 1:
            return 1
    return 0

ok = 0
ng = N + 1
while ng - ok > 1:
    mid = (ok + ng) // 2
    if check(mid) == -1:
        ok = mid
    else:
        ng = mid
l = ok


ok = N + 1
ng = 0
while ok - ng > 1:
    mid = (ok + ng) // 2
    if check(mid) == 1:
        ok = mid
    else:
        ng = mid

r = ng

print(r - l)

# Buggy version:
N,Q = map(int,input().split())
s = ["LE"] + list(input()) + ["RE"]
td = []
lend = 0
rend = N-1
for i in range(Q):
    t,d = map(str,input().split())
    td.append([t,d])
td = td[::-1]
for i,j in td:
    if j == "L" and i == s[lend]:#左端を1つずらす
        lend += 1
    if j == "R" and i == s[rend]:#右端を1つずらす
        rend -= 1  
    if j == "L" and i == s[rend+1] and rend < N-1:#1つ前の位置を参照して右に戻す
        rend += 1
    if j == "R" and i == s[lend-1] and lend > 0:#1つ前の位置を参照して左に戻す
        lend -= 1
    if lend > rend:
        break

print(rend - lend + 1)

# Fixed version:
N,Q = map(int,input().split())
s =input()
td = []
lend = 0
rend = N-1

for i in range(Q):
    t,d = map(str,input().split())
    td.append([t,d])
td = td[::-1]
for i,j in td:
    if j == "L" and i == s[lend]:#左端を1つずらす
        lend += 1
    if j == "R" and i == s[rend]:#右端を1つずらす
        rend -= 1  
    if rend < (N-1) and j == "L" and i == s[rend+1]:#1つ前の位置を参照して右に戻す
        rend += 1
    if lend > 0 and j == "R" and i == s[lend-1]:#1つ前の位置を参照して左に戻す
        lend -= 1
    if lend > rend:
        break

print(rend - lend + 1)

# Buggy version:
n,q=map(int,input().split())
s=input()
p=[input().split() for i in range(q)]
def judge_l(x):
    for(k,v)in p:
        if k==s[x]:
            if v=="L":
                x-=1
            else:
                x+=1
        if x==-1:
            return True
        elif x==n:
            return False
    return False

def judge_r(x):
    for(k,v)in p:
        if k==s[x]:
            if v=="L":
                x-=1
            else:
                x+=1
        if x==-1:
            return False
        elif x==n:
            return True
    return False

ok=-1
ng=n

while ok-ng>1:
    mid=(ok+ng)//2
    if judge_l(mid):
        ok=mid
    else:
        ng=mid
l=ok+1

ok=n
ng=-1

while ok-ng>1:
    mid=(ok+ng)//2
    if judge_r(mid):
        ok=mid
    else:
        ng=mid
r=n-ok
#print(l,r)
print(max(n-l-r,0))

# Fixed version:
n,q=map(int,input().split())
s=input()
p=[input().split() for i in range(q)]
def judge_l(x):
    for(k,v)in p:
        if k==s[x]:
            if v=="L":
                x-=1
            else:
                x+=1
        if x==-1:
            return True
        elif x==n:
            return False
    return False

def judge_r(x):
    for(k,v)in p:
        if k==s[x]:
            if v=="L":
                x-=1
            else:
                x+=1
        if x==-1:
            return False
        elif x==n:
            return True
    return False

ok=-1
ng=n

while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if judge_l(mid):
        ok=mid
    else:
        ng=mid
l=ok+1

ok=n
ng=-1

while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if judge_r(mid):
        ok=mid
    else:
        ng=mid
r=n-ok
#print(l,r)
print(max(n-l-r,0))

# Buggy version:
N,M=map(int,input().split())
a = input()
k = []
for i in range(M):
  ta,tb=map(str,input().split())
  k.append([ta,tb])
k = k[::-1]
s = 0
t = N-1
c=0
for i, j in k:
  if j == "R" and i == a[t]:
    t-=1
    c+=1
  elif j == "L" and i == a[s]:
    s+=1
  if j == "R" and i == a[s]:
    s-=1
  elif j == "S" and i == a[t]:
    t+=1
    c-=1
  if s+c==N and N!=0:break
if N>1:print(N-s-c)
else: 
  if s+c>1:print(1)
  else:print(0)



# Fixed version:
N,M=map(int,input().split())
a = "a"+input()+"q"
k = []
for i in range(M):
  ta,tb=map(str,input().split())
  k.append([ta,tb])
k = k[::-1]
s = 1
t = N
c=0
for i, j in k:
  if j == "R" and i == a[t]:
    t-=1
    c+=1
  if j == "L" and i == a[s]:
    s+=1
  if j == "R" and i == a[s-1] and s!= 1:
    s-=1
  if j == "L" and i == a[t+1] and t!= N:
    t+=1
    c-=1
  if s+c-1==N and N!=0:break
if N>1:print(N-s-c-1+2)
else: 
  if s+c+1>1:print(1)
  else:print(0)

# Buggy version:
n,q = map(int,input().split())
s = input()
l = [list(input().split()) for i in range(q)]

def rb(x):
    st = x
    for i in range(q):
        if st == n:
            return True
        if st == -1:
            return False
        if l[i][0] == s[st]:
            if l[i][1] == "R":
                st += 1
            else:
                st -= 1
    if st == n:
        return True
    return False
def lb(x):
    st = x
    for i in range(q):
        if st == -1:
            return True
        if st == n:
            return False
        if l[i][0] == s[st]:
            if l[i][1] == "L":
                st += 1
            else:
                st -= 1
    if st == -1:
        return True
    return False
def bs():
    p = n
    ng = -1
    while abs(p - ng) > 1:
        m = (p+ng)//2
        if rb(m):
            p = m
        else:
            ng = m
    return p
def bs2():
    p = -1
    ng = n
    while abs(p-ng) > 1:
        m = (p+ng)//2
        if lb(m):
            p = m
        else:
            ng = m
    return p
print(bs() - bs2() -1)




# Fixed version:
n,q = map(int,input().split())
s = input()
l = [list(input().split()) for i in range(q)]

def rb(x):
    st = x
    for i in range(q):
        if st == n:
            return True
        if st == -1:
            return False
        if l[i][0] == s[st]:
            if l[i][1] == "R":
                st += 1
            else:
                st -= 1
    if st == n:
        return True
    return False
def lb(x):
    st = x
    for i in range(q):
        if st == -1:
            return True
        if st == n:
            return False
        if l[i][0] == s[st]:
            if l[i][1] == "R":
                st += 1
            else:
                st -= 1
    if st == -1:
        return True
    return False
def bs():
    p = n
    ng = -1
    while abs(p - ng) > 1:
        m = (p+ng)//2
        if rb(m):
            p = m
        else:
            ng = m
    return p
def bs2():
    p = -1
    ng = n
    while abs(p-ng) > 1:
        m = (p+ng)//2
        if lb(m):
            p = m
        else:
            ng = m
    return p
print(bs() - bs2() -1)

# Buggy version:
import sys
n,q=map(int,input().split())
s=input()
l=[[i for i in l.split()] for l in sys.stdin]
def right_fallen(x):
  stack=x
  for i in range(n):
    if stack==n:
      return True
    if stack==-1:
      return False
    if l[i][0]==x[stack]:
      if l[i][1]=='R':
        stack+=1
      else:
        stack-=1
  return False
def right_fallen(x):
  stack=x
  for i in range(n):
    if stack==n:
      return False
    if stack==-1:
      return True
    if l[i][0]==x[stack]:
      if l[i][1]=='R':
        stack+=1
      else:
        stack-=1
  return False
def binary_search():
  ok=n
  ng=-1
  while(abs(ok-ng)>1):
    mid=(ok+ng)//2
    if right_fallen(mid):
      ok=mid
    else:
      ng=mid
  return ok
def binary_search2():
  ok=-1
  ng=n
  while(abs(ok-ng)>1):
    mid=(ok+ng)//2
    if left_fallen(mid):
      ok=mid
    else:
      ng=mid
  return ok
print(binary_search-binarysearch2-1)

# Fixed version:
import sys
n,q=map(int,input().split())
s=input()
l=[[i for i in l.split()] for l in sys.stdin]
def right_fallen(x):
  stack=x
  for i in range(q):
    if stack==n:
      return True
    if stack==-1:
      return False
    if l[i][0]==s[stack]:
      if l[i][1]=='R':
        stack+=1
      else:
        stack-=1
  if stack==n:
    return True
  return False
def left_fallen(x):
  stack=x
  for i in range(q):
    if stack==n:
      return False
    if stack==-1:
      return True
    if l[i][0]==s[stack]:
      if l[i][1]=='R':
        stack+=1
      else:
        stack-=1
  if stack==-1:
    return True
  return False
def binary_search():
  ok=n
  ng=-1
  while(abs(ok-ng)>1):
    mid=(ok+ng)//2
    if right_fallen(mid):
      ok=mid
    else:
      ng=mid
  return ok
def binary_search2():
  ok=-1
  ng=n
  while(abs(ok-ng)>1):
    mid=(ok+ng)//2
    if left_fallen(mid):
      ok=mid
    else:
      ng=mid
  return ok
print(binary_search()-binary_search2()-1)

# Buggy version:
N, Q = map(int, input().split())
s = input()

X = []
for i in range(Q):
    t, d = input().split()
    if d == 'L':
        X.append([t, -1])
    else:
        X.append([t, 1])
        
def check(m):
    i = m
    for x in X:
        if x[0] == s[i]:
            i += x[1]
            if i < 0:
                return -1
            if i >= N:
                return 1
    return 0

L, R = 0, N
while R - L > 1:
    M = (L + R) // 2
    if check(M) < 0:
        L = M
    else:
        R = M  
A = L

L, R = 0, N
while R - L > 1:
    M = (L + R) // 2
    if check(M) >= 0:
        R = M
    else:
        L = M
B = R

print(B - A + 1)

# Fixed version:
N, Q = map(int, input().split())
s = input()

X = []
for i in range(Q):
    t, d = input().split()
    if d == 'L':
        X.append([t, -1])
    else:
        X.append([t, 1])
        
def check(m):
    i = m
    for x in X:
        if x[0] == s[i]:
            i += x[1]
            if i < 0:
                return -1
            if i >= N:
                return 1
    return 0

L, R = -1, N
while R - L > 1:
    M = (L + R) // 2
    if check(M) < 0:
        L = M
    else:
        R = M  
A = L

L, R = -1, N
while R - L > 1:
    M = (L + R) // 2
    if check(M) > 0:
        R = M
    else:
        L = M
B = R

print(B - A - 1)

# Buggy version:
N,Q=map(int,input().split())
S=list(input())
S=[0]+S+[N+1]
TD=[list(input().split()) for _ in range(Q)]

def solve(place):
    if place == 0:
        return ('Left')
    elif place == N+1:
        return ('Right')
    for td in TD:
        char,to=td
        if S[place]!=char:continue

        if to=='L':place-=1
        elif to=='R':place+=1

        if place==-1:
            return('Left')
        elif place==N:
            return('Right')
    return('Not Drop')

l,r=0,N+1
L,R=0,N+1

while abs(r-l)>1:
    m=(l+r)//2
    if solve(m)=='Left':
        L=m
        l,r=m,r
    else:
        l,r=l,m

l,r=0,N+1

while abs(r-l)>1:
    m=(l+r)//2
    if solve(m)=='Right':
        R=m
        l,r=l,m
    else:
        l,r=m,r

print(max(R-L-1,0))


# Fixed version:
N,Q=map(int,input().split())
S=list(input())
S=[0]+S+[N+1]
TD=[list(input().split()) for _ in range(Q)]

def solve(place):
    if place == 0:
        return ('Left')
    elif place == N+1:
        return ('Right')
    for td in TD:
        char,to=td
        if S[place]!=char:continue

        if to=='L':place-=1
        elif to=='R':place+=1

        if place==0:
            return('Left')
        elif place==N+1:
            return('Right')
    return('Not Drop')

l,r=0,N+1
L,R=0,N+1

while abs(r-l)>1:
    m=(l+r)//2
    if solve(m)=='Left':
        L=m
        l,r=m,r
    else:
        l,r=l,m

l,r=0,N+1

while abs(r-l)>1:
    m=(l+r)//2
    if solve(m)=='Right':
        R=m
        l,r=l,m
    else:
        l,r=m,r

print(max(R-L-1,0))

# Buggy version:
n, q = map(int, input().split())
s = input()
ts, ds = [], []
for _ in range(q):
    t, d = input().split()
    ts.append(t)
    ds.append(d)

def reachable(n, dest):
    for t, d in zip(ts, ds):
        if s[n] == t:
            n = n - 1 if d == 'l' else n + 1
        if n == dest:
            return true
        elif not 0 <= n <= len(s) - 1:
            return false
    return false

l, r = -1, n
while r - l > 1:
    m = l + (r - l) // 2
    if reachable(m, -1): l = m
    else: r = m
ldel = l + 1

l, r = -1, n
while r - l > 1:
    m = l + (r - l) // 2
    if reachable(m, n): r = m
    else: l = m
rdel = n - r

print(max(n - (ldel + rdel), 0))

# Fixed version:
n, q = map(int, input().split())
s = input()
ts, ds = [], []
for _ in range(q):
    t, d = input().split()
    ts.append(t)
    ds.append(d)

def reachable(n, dest):
    for t, d in zip(ts, ds):
        if s[n] == t:
            n = n - 1 if d == 'L' else n + 1
        if n == dest:
            return True
        elif not 0 <= n <= len(s) - 1:
            return False
    return False


l, r = -1, n
while r - l > 1:
    m = l + (r - l) // 2
    if reachable(m, -1):
        l = m
    else:
        r = m
ldel = l + 1

l, r = -1, n
while r - l > 1:
    m = l + (r - l) // 2
    if reachable(m, n):
        r = m
    else:
        l = m
rdel = n - r

print(max(n - (ldel + rdel), 0))

# Buggy version:
n,q = (int(i) for i in input().split())
s = '#'+input()+'#'
l,r = 0,0
a = [list(input().split()) for i in range(q)]
for i in range(q):
    t,d = a[i]
    if s[l+1] == t and d =='L':
        l += 1
    elif s[l] == t and d == 'R':
        l -= 1
    if s[r] == t and d == 'L':
        r += 1
    elif s[r-1] == t and d == 'R':
        r -= 1
print(max(0,r-l-1))

# Fixed version:
n,q=map(int,input().split())
s="0"+input()+"0"
a=[list(input().split())for i in range(q)]
l=0
r=n+1
for i in range(q-1,-1,-1):
    t,d=a[i]
    if t==s[l] and d=="R":
        l-=1
    elif t==s[l+1] and d=="L":
        l+=1
    if t==s[r] and d=="L":
        r+=1
    elif t==s[r-1] and d=="R":
        r-=1
print(max(0,r-l-1))

# Buggy version:
# -*- coding: utf-8 -*-
def vanish_golem(pos, n, s, spells):
    for t, d in spells:
        if pos < 0:
            return -1
        if pos >= n:
            return 1

        if s[pos] == t:
            pos += 1 if d == 'R' else -1

    if pos < 0:
        return -1
    elif pos >= n:
        return 1
    else:
        return 0


def search_vanish_golem(n, s, spells, th):
    l = 0
    h = n-1

    while l <= h:
        t = (l+h) // 2
        v = vanish_golem(t, n, s, spells)
        if v < th:
            l = t + 1
        elif v > th:
            h = t - 1

    return l


n, q = map(int, input().split())
s = input()

td = [input().split() for _ in range(q)]

l_cnt = search_vanish_golem(n, s, td, 0)
r_cnt = n - search_vanish_golem(n, s, td, 1)

print(n - l_cnt - r_cnt)


# Fixed version:
# -*- coding: utf-8 -*-
def vanish_golem(pos, n, s, spells):
    for t, d in spells:
        if pos < 0:
            return -1
        if pos >= n:
            return 1

        if s[pos] == t:
            pos += 1 if d == 'R' else -1

    if pos < 0:
        return -1
    elif pos >= n:
        return 1
    else:
        return 0


def search_vanish_golem(n, s, spells, th):
    l = 0
    h = n-1

    while l <= h:
        t = l + (h - l) // 2
        v = vanish_golem(t, n, s, spells)
        if v < th:
            l = t + 1
        else:
            h = t - 1

    return l


n, q = map(int, input().split())
s = input()

td = [input().split() for _ in range(q)]

l_cnt = search_vanish_golem(n, s, td, 0)
r_cnt = n - search_vanish_golem(n, s, td, 1)

print(n - l_cnt - r_cnt)

# Buggy version:
N,Q = map(int,input().split())
s = input()
info = [input().split() for _ in range(Q)]

def left_out():
    left = -1; right = N
    while right - left > 1:
        mid = (left+right) // 2
        if judge(mid) == -1:
            left = mid
        else:
            right = mid
    return left

def right_out():
    left = -1; right = N
    while right - left > 1:
        mid = (left+right) // 2
        if judge(mid) == 1:
            right = mid
        else:
            left = mid
    return right

def judge(i):
    now = i
    for t, d in info:
        if s[now] == t:
            if d == "L":
                now -= 1
            elif d == "R":
                now += 1
        if now < 0:
            return -1
        elif N <= now:
            return 1
    return 0
    
left = left_out()
right = right_out()
print(left,right)
ans = right - left - 1
print(ans)

# Fixed version:
N,Q = map(int,input().split())
s = input()
info = [input().split() for _ in range(Q)]

def left_out():
    left = -1; right = N
    while right - left > 1:
        mid = (left+right) // 2
        if judge(mid) == -1:
            left = mid
        else:
            right = mid
    return left

def right_out():
    left = -1; right = N
    while right - left > 1:
        mid = (left+right) // 2
        if judge(mid) == 1:
            right = mid
        else:
            left = mid
    return right

def judge(i):
    now = i
    for t, d in info:
        if s[now] == t:
            if d == "L":
                now -= 1
            elif d == "R":
                now += 1
        if now < 0:
            return -1
        elif N <= now:
            return 1
    return 0
    
left = left_out()
right = right_out()
ans = right - left - 1
print(ans)

# Buggy version:
n,q=map(int,input().split())
c=input()
s=[input().split() for _ in [0]*q]
def judge(x,opt):
    for y in s:
        if y[0]==c[x]:
            if y[1]=='L':
                x-=1
            else:
                x+=1
            if not opt:
                if x>=n:
                    return False
                elif x<0:
                    return True
            else:
                if x<0:
                    return False
                elif x>=n:
                    return True
    return False
def bisect(l,r,opt):
    print(l,r)
    center=(r+l)//2
    if judge(center,opt):
        if r-l<=1:
            if not opt:
                return l+1
            else:
                return  n-l
        if not opt:
            return bisect(center+1,r,opt)
        else:
            return bisect(l,center,opt)
    else:
        if r-l<=1:
            if not opt:
                return l
            else:
                return n-r
        if not opt:
            return bisect(l,center,opt)
        else:
            return bisect(center+1,r,opt)
def func():
    l=bisect(0,n,0)
    r=bisect(0,n,1)
    print(l,r)
    print(n-l-r)
func()

# Fixed version:
n,q=map(int,input().split())
c=input()
s=[input().split() for _ in [0]*q]
def judge(x,opt):
    for y in s:
        if y[0]==c[x]:
            if y[1]=='L':
                x-=1
            else:
                x+=1
            if not opt:
                if x>=n:
                    return False
                elif x<0:
                    return True
            else:
                if x<0:
                    return False
                elif x>=n:
                    return True
    return False
def bisect(l,r,opt):
    center=(r+l)//2
    if judge(center,opt):
        if r-l<=1:
            if not opt:
                return l+1
            else:
                return  n-l
        if not opt:
            return bisect(center+1,r,opt)
        else:
            return bisect(l,center,opt)
    else:
        if r-l<=1:
            if not opt:
                return l
            else:
                return n-r
        if not opt:
            return bisect(l,center,opt)
        else:
            return bisect(center+1,r,opt)
def func():
    l=bisect(0,n,0)
    r=bisect(0,n,1)
    print(n-l-r)
func()

# Buggy version:
n, q = map(int, input().split())
s = input()
tds = list(tuple(input().split()) for _ in range(q))

left = 0
right = n-1

for t, d in reversed(tds):
    if d=="L":
        if t==s[left]:
            left = left+1
            if left > n-1:
                right = 0
                break
        if right < n-1 and t==s[right+1]:
            right = min(right+1, n-1)

    if d=="R":
        if t==s[right]:
            right = right-1
            if 0 < right:
                right = 0
                break
        if left > 0 and t==s[left-1]:
            left = max(left-1, 0)

print(max(right - left + 1, 0))


# Fixed version:
n, q = map(int, input().split())
s = input()
tds = list(tuple(input().split()) for _ in range(q))

left = 0
right = n-1

for t, d in reversed(tds):
    if d=="L":
        if t==s[left]:
            left = left+1
            if left > n-1:
                right = 0
                left = n-1
                break
        if right < n-1 and t==s[right+1]:
            right = min(right+1, n-1)

    if d=="R":
        if t==s[right]:
            right = right-1
            if 0 > right:
                right = 0
                left = n-1
                break
        if left > 0 and t==s[left-1]:
            left = max(left-1, 0)

print(max(right - left + 1, 0))

# Buggy version:
N, Q = list(map(int, input().split()))
s = input()

lt = []
ld = []

for q in range(Q):
    t, d = input().split()
    lt.append(t)
    ld.append(d)

pl = -1
pr = N

for q in range(Q-1, -1, -1):
    if ld[q] == 'L':
        if pl < N - 1 and s[pl + 1] == lt[q]:
            pl += 1
        if pr < N - 1 and s[pr + 1] == lt[q]:
            pr += 1
    else:
        if pr > 0 and s[pr - 1] == lt[q]:
            pr -= 1
        if pl > 0 and s[pl - 1] == lt[q]:
            pl -= 1

print(max(pr - pl -1, 0))


# Fixed version:
N, Q = list(map(int, input().split()))
s = input()

lt = []
ld = []

for q in range(Q):
    t, d = input().split()
    lt.append(t)
    ld.append(d)

pl = -1
pr = N

for q in range(Q-1, -1, -1):
    if ld[q] == 'L':
        if pl < N - 1 and s[pl + 1] == lt[q]:
            pl += 1
        if pr <= N - 1 and s[pr] == lt[q]:
            pr += 1
    else:
        if pr > 0 and s[pr - 1] == lt[q]:
            pr -= 1
        if pl >= 0 and s[pl] == lt[q]:
            pl -= 1

print(max(pr - pl -1, 0))

# Buggy version:
from collections import Counter
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()

N, Q = tuple(li())
S = ns()
ops = []
for i in range(Q):
    ops.append(tuple(ls()))

head, tail = 0, N-1
while(ops):
    cur = ops.pop()
    if cur[1] == 'L':
        if S[head] == cur[0]:
            head += 1
    elif cur[1] == 'R':
        if S[tail] == cur[0]:
            tail -= 1
alive = tail - head + 1
print("tail:{}, head:{}".format(tail, head))
print(max(alive, 0))


# Fixed version:
from collections import Counter
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()

N, Q = tuple(li())
S = ns()
ops = []
for i in range(Q):
    ops.append(tuple(ls()))

head, tail = 0, N-1
while(ops):
    cur = ops.pop()
    if cur[1] == 'L' and head <N:
        if S[head] == cur[0]:
            head += 1
        if tail <= N-2 and S[tail + 1] == cur[0]:
            tail = min(tail + 1, N-1)
    elif cur[1] == 'R' and tail >= 0:
        if S[tail] == cur[0]:
            tail -= 1
        if head >= 1 and S[head - 1] == cur[0]:
            head = max(0, head-1)
alive = tail - head + 1
# print("tail:{}, head:{}".format(tail, head))
print(max(alive, 0))

# Buggy version:
N,Q=map(int,input().split())
S=input()
table=[]
for i in range(Q):
    t,d=input().split()
    table.append((t,d))
def f(x):
    u=x
    for t,d in table:
        if S[u]==t:
            if d=='L':
                u-=1
            else:
                u+=1
        if u==-1:
            return 0
        if u==N:
            return 1
    return 2
mi=0
ma=N-1
while ma-mi>1:
    mid=(ma+mi)//2
    if f(mid)==0:
        mi=mid
    else:
        ma=mid
left=mi
mi=0
ma=N-1
while ma-mi>1:
    mid=(ma+mi)//2
    if f(mid)==1:
        ma=mid
    else:
        mi=mid
right=ma
print(left,right)
ans=(left+1)+(N-right)
num=N-ans
if left==0:
    if f(0)==2:
        num+=1
if right==N-1:
    if f(N-1)==2:
        num+=1
print(num)

# Fixed version:
N,Q=map(int,input().split())
S=input()
table=[]
for i in range(Q):
    t,d=input().split()
    table.append((t,d))
def f(x):
    u=x
    for t,d in table:
        if S[u]==t:
            if d=='L':
                u-=1
            else:
                u+=1
        if u==-1:
            return 0
        if u==N:
            return 1
    return 2
mi=0
ma=N-1
while ma-mi>1:
    mid=(ma+mi)//2
    if f(mid)==0:
        mi=mid
    else:
        ma=mid
left=mi
mi=0
ma=N-1
while ma-mi>1:
    mid=(ma+mi)//2
    if f(mid)==1:
        ma=mid
    else:
        mi=mid
right=ma
#print(left,right)
ans=(left+1)+(N-right)
num=N-ans
if left==0:
    if f(0)==2:
        num+=1
if right==N-1:
    if f(N-1)==2:
        num+=1
print(num)