;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro nlet (name args &body body)
  (labels ((ensure-list (x) (if (listp x) x (list x))))
    (let ((args (mapcar #'ensure-list args)))
      `(labels ((,name ,(mapcar #'car args) ,@body))
         (,name ,@(mapcar #'cadr args))))))

(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (terminate-char #\Space))
  "Note that the returned string will be reused."
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let ((,buffer (load-time-value (make-string ,buffer-size
                                                  :element-type 'base-char))))
       (declare (simple-base-string ,buffer))
       (loop for ,character of-type base-char =
                #-swank (code-char (read-byte ,in nil #.(char-code #\Newline)))
                #+swank (read-char ,in nil #\Newline)
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (when (< ,idx ,buffer-size)
                       (setf (schar ,buffer ,idx) ,terminate-char))
                     (return (values ,buffer ,idx))))))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (terminate-char #\Space))
  (declare (simple-base-string buffer-string))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (schar buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (schar buffer-string idx) terminate-char))
                (return (values buffer-string idx))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Body

(declaim (inline to-code))
(defun to-code (c) (- (char-code C) 65))

(defun main ()
  (let* ((n (read))
         (q (read))
         (s (make-string n :element-type 'base-char))
         (seq (make-array n :element-type 'uint7))
         (operations (make-array q :element-type '(cons int32 int32))))
    (declare (uint32 n q))
    (read-line-into s)
    (dotimes (i n)
      (setf (aref seq i) (to-code (aref s i))))
    (dotimes (i q)
      (let* ((line (buffered-read-line 3))
             (code (to-code (char line 0)))
             (direction (if (char= #\L (char line 2)) -1 1)))
        (setf (aref operations i) (cons code direction))))
    (labels ((escape-right-p (pos)
               (declare (int32 pos))
               (dotimes (i q nil)
                 (when (= pos -1)
                   (return nil))
                 (when (= pos n)
                   (return t))
                 (let* ((op (aref operations i))
                        (dir (cdr op))
                        (object (car op)))
                   (declare (int32 object dir))
                   (when (= (aref seq pos) object)
                     (incf pos dir))))
               (if (= pos n) t nil))
             (escape-left-p (pos)
               (declare (int32 pos))
               (dotimes (i q nil)
                 (when (= pos -1)
                   (return t))
                 (when (= pos n)
                   (return nil))
                 (let* ((op (aref operations i))
                        (dir (cdr op))
                        (object (car op)))
                   (declare (int32 object dir))
                   (when (= (aref seq pos) object)
                     (incf pos dir))))
               (if (= pos -1) t nil)))
      (let ((p (if (escape-right-p 0)
                   0
                   (if (not (escape-right-p (- n 1)))
                       n
                       (nlet bisect ((ng 0) (ok n))
                         (declare (int32 ng ok))
                         (if (<= (- ok ng) 1)
                             ok
                             (let ((mid (floor (+ ng ok) 2)))
                               (if (escape-right-p mid)
                                   (bisect ng mid)
                                   (bisect mid ok))))))))
            (q (if (escape-left-p (- n 1))
                   (- n 1)
                   (if (not (escape-left-p 0))
                       -1
                       (nlet bisect ((ok 0) (ng (- n 1)))
                         (declare (int32 ng ok))
                         (if (<= (- ng ok) 1)
                             ok
                             (let ((mid (floor (+ ng ok) 2)))
                               (if (escape-left-p mid)
                                   (bisect mid ng)
                                   (bisect ok mid)))))))))
        (println (if (<= p q)
                     0
                     (- p q 1)))))))

#-swank(main)


Measured memory_bytes: 31332