#include <bits/stdc++.h>   using namespace std;   char F1[2000000];   char F2[2000000];   void computeLPSArray(string pat, int M, int* lps);   void KMPSearch(string pat, string txt, vector<int>& st, vector<int>& end) {     int M = pat.size();     int N = txt.size();     int lps[M];     computeLPSArray(pat, M, lps);     int i = 0;     int j = 0;     while (i < N) {       if (pat[j] == txt[i]) {         j++;         i++;       }       if (j == M) {         end.push_back(i - 1);         st.push_back(i - M);         j = lps[j - 1];       } else if (i < N && pat[j] != txt[i]) {         if (j != 0)           j = lps[j - 1];         else           i = i + 1;       }     }   }   void computeLPSArray(string pat, int M, int* lps) {     int len = 0;     lps[0] = 0;     int i = 1;     while (i < M) {       if (pat[i] == pat[len]) {         len++;         lps[i] = len;         i++;       } else {         if (len != 0) {           len = lps[len - 1];         } else {           lps[i] = 0;           i++;         }       }     }   }   long long int Stoi(string s) {     long long int ans = 0;     for (int i = 0; i < s.size(); i++) ans = ans * 10 + s[i] -  0 ;     return ans;   }   string itos(long long int num) {     if (num == 0) return  0 ;     string ans =   ;     while (num) {       int digit = num % 10;       char ch =  0  + digit;       ans = ch + ans;       num = num / 10;     }     return ans;   }   long long int numBefore(int i, string& s) {     int indx = i - 1;     while (s[indx] >=  0  && s[indx] <=  9  && indx >= 0) indx--;     indx++;     int len = i - indx;     long long int num = Stoi(s.substr(indx, len));     return num;   }   int charAfter(int i, string& s) {     if (i == s.size() - 1) return -1;     int indx = i + 1;     while (s[indx] >=  0  && s[indx] <=  9  && indx >= 0) indx++;     return indx;   }   int main() {     int m, n;     cin >> n >> m;     long long int prevFreq = 0;     char prevCh =  Q ;     string Final =   ;     int ptr1 = 0, ptr2 = 0;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       int size = s.size();       int indx = size - 2;       ;       string num1 = s.substr(0, indx);       long long int frq = Stoi(num1);       char Chr = s[indx + 1];       if (Chr == prevCh)         prevFreq += frq;       else {         if (prevCh !=  Q ) {           string s1 = itos(prevFreq) + prevCh;           for (int i = 0; i < s1.size(); i++) {             F1[ptr1] = s1[i];             ptr1++;           }         }         prevFreq = frq;         prevCh = Chr;       }     }     string s1 = itos(prevFreq) + prevCh;     for (int i = 0; i < s1.size(); i++) {       F1[ptr1] = s1[i];       ptr1++;     }     F1[ptr1] = 0;     Final = F1;     prevFreq = 0;     prevCh =  Q ;     string Final2 =   ;     for (int i = 0; i < m; i++) {       string s;       cin >> s;       int indx = s.find( - );       string num1 = s.substr(0, indx);       long long int frq = Stoi(num1);       char Chr = s[indx + 1];       if (Chr == prevCh)         prevFreq += frq;       else {         if (prevCh !=  Q ) {           string s2 = itos(prevFreq) + prevCh;           for (int i = 0; i < s2.size(); i++) {             F2[ptr2] = s2[i];             ptr2++;           }         }         prevFreq = frq;         prevCh = Chr;       }     }     string s2 = itos(prevFreq) + prevCh;     for (int i = 0; i < s2.size(); i++) {       F2[ptr2] = s2[i];       ptr2++;     }     Final2 = F2;     vector<int> st, end;     int indx = 0;     int indx2 = Final2.size() - 2;     while (Final2[indx] >=  0  && Final2[indx] <=  9 ) indx++;     while (indx2 >= 0 && Final2[indx2] >=  0  && Final2[indx2] <=  9 ) indx2--;     long long int ans = 0;     if (indx2 >= 0) {       string toSrch = Final2.substr(indx, indx2 - indx + 1);       KMPSearch(toSrch, Final, st, end);       for (int i = 0; i < st.size(); i++) {         long long int Aval = numBefore(st[i], Final);         long long int Req = numBefore(indx, Final2);         int i2 = charAfter(end[i], Final);         int i3 = charAfter(indx2, Final2);         if (i2 >= 0 && i3 >= 0 && Final2[i3] == Final[i2]) {           long long int Aval2 = numBefore(i2, Final);           long long int Req2 = numBefore(i3, Final2);           if (Aval >= Req && Aval2 >= Req2) ans++;         }       }     } else {       char ch = Final2[Final2.size() - 1];       for (int i = 0; i < Final.size(); i++) {         if (Final[i] == ch) {           long long int Aval = numBefore(i, Final);           long long int Req = numBefore(Final2.size() - 1, Final2);           if (Aval >= Req) ans = ans + (Aval - Req + 1);         }       }     }     cout << ans << endl;   }