Buggy python submission (verdict: Time Limit Exceeded):
N,Q,S,*L = open(0).read().split()
N,Q = int(N),int(Q)
ans = N
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  i = s
  p = S[i]
  for t,d in zip(*[iter(L)]*2):
    if p!=t:
      continue
    if d=='L':
      if i==0:
        l = s
        break
      i -= 1
      p = S[i]
    else:
      if i==N-1:
        r = s
        break
      i += 1
      p = S[i]
  else:
    r = s
ans -= r
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  i = s
  p = S[i]
  for t,d in zip(*[iter(L)]*2):
    if p!=t:
      continue
    if d=='L':
      if i==0:
        l = s
        break
      i -= 1
      p = S[i]
    else:
      if i==N-1:
        r = s
        break
      i += 1
      p = S[i]
  else:
    l = s

ans -= N-r  
print(ans)

Fixed submission:
N, Q, S, *L = open(0).read().split()
N = int(N)
Q = int(Q)
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if m<=-1:
      l = s
      break
    if N<=m:
      r = s
      break
    p = S[m]
  else:
    r = s
ans = l+1
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if N<=m:
      r = s
      break
    if m<=-1:
      l = s
      break
    p = S[m]
  else:
    l = s

ans += N-r
print(N-ans)

Buggy python submission (verdict: Runtime Error):
N, Q, S, *L = open(0).read().split()
N = int(N)
Q = int(Q)
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if m<=-1:
      l = s
      break
    p = S[m]
  else:
    r = s
ans = l+1
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if N<=m:
      r = s
      break
    p = S[m]
  else:
    l = s

ans += N-r
print(N-ans)

Fixed submission:
N, Q, S, *L = open(0).read().split()
N = int(N)
Q = int(Q)
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if m<=-1:
      l = s
      break
    if N<=m:
      r = s
      break
    p = S[m]
  else:
    r = s
ans = l+1
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if N<=m:
      r = s
      break
    if m<=-1:
      l = s
      break
    p = S[m]
  else:
    l = s

ans += N-r
print(N-ans)

Buggy python submission (verdict: Runtime Error):
N, Q, S, *L = open(0).read().split()
N = int(N)
Q = int(Q)
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if m<=-1:
      l = s
      break
    p = S[m]
  else:
    r = s
ans = l+1
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if N<=m:
      r = s
      break
    p = S[m]
  else:
    l = s

ans += N-r
print(N-ans)

Fixed submission:
N, Q, S, *L = open(0).read().split()
N = int(N)
Q = int(Q)
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if m<=-1:
      l = s
      break
    if N<=m:
      r = s
      break
    p = S[m]
  else:
    r = s
ans = l+1
l = -1
r = N
while l+1<r:
  s = (l+r)//2
  p = S[s]
  m = s
  for t,d in zip(*[iter(L)]*2):
    if p==t:
      if d=='L':
        m -= 1
      else:
        m += 1
    if N<=m:
      r = s
      break
    if m<=-1:
      l = s
      break
    p = S[m]
  else:
    l = s

ans += N-r
print(N-ans)