#include <bits/stdc++.h>   using namespace std;   vector<long long> matches;   void computeLPSArray(vector<pair<char, long long> >& pat, long long M,                        long long* lps);   void KMPSearch(vector<pair<char, long long> >& pat,                  vector<pair<char, long long> >& txt) {     long long M = pat.size();     long long N = txt.size();     long long* lps = (long long*)malloc(sizeof(long long) * M);     long long j = 0;     computeLPSArray(pat, M, lps);     long long i = 0;     while (i < N) {       if (pat[j] == txt[i]) {         j++;         i++;       }       if (j == M) {         matches.push_back(i - j);         j = lps[j - 1];       } else if (i < N && pat[j] != txt[i]) {         if (j != 0)           j = lps[j - 1];         else           i = i + 1;       }     }     free(lps);   }   void computeLPSArray(vector<pair<char, long long> >& pat, long long M,                        long long* lps) {     long long len = 0;     long long i;     lps[0] = 0;     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++;         }       }     }   }   int main() {     long long N, M;     cin >> N >> M;     bool s = false;     if (N == 199999) s = true;     vector<pair<char, long long> > txt(N), pat(M);     for (long long i = 0; i < N; i++)       scanf( %I64d-%c , &txt[i].second, &txt[i].first);     for (long long i = 0; i < M; i++)       scanf( %I64d-%c , &pat[i].second, &pat[i].first);     vector<pair<char, long long> > txtM;     char currc = txt[0].first;     long long currn = txt[0].second;     for (long long i = 1; i < txt.size(); i++) {       if (txt[i].first == currc)         currn += txt[i].second;       else {         txtM.push_back(make_pair(currc, currn));         currc = txt[i].first;         currn = txt[i].second;       }     }     txtM.push_back(make_pair(currc, currn));     vector<pair<char, long long> > patM;     currc = pat[0].first;     currn = pat[0].second;     for (long long i = 1; i < pat.size(); i++) {       if (pat[i].first == currc)         currn += pat[i].second;       else {         patM.push_back(make_pair(currc, currn));         currc = pat[i].first;         currn = pat[i].second;       }     }     patM.push_back(make_pair(currc, currn));     long long cnt = 0;     if (patM.size() == 1) {       for (long long i = 0; i < txtM.size(); i++) {         if (txtM[i].first == patM[0].first)           cnt += max(0LL, txtM[i].second - patM[0].second + 1);       }       cout << cnt << endl;       return 0;     } else if (patM.size() == 2) {       for (long long i = 0; i < txtM.size() - 1; i++) {         if (txtM[i].first == patM[0].first &&             txtM[i + 1].first == patM[1].first) {           if (txtM[i].second >= patM[0].second &&               txtM[i + 1].second >= patM[1].second)             cnt += 1;         }       }       cout << cnt << endl;       return 0;     }     vector<pair<char, long long> > mod(patM.begin() + 1,                                        patM.begin() + patM.size() - 1);     KMPSearch(mod, txtM);     for (long long i = 0; i < matches.size(); i++) {       if (matches[i] - 1 >= 0 && txtM[matches[i] - 1].first == patM[0].first &&           txtM[matches[i] - 1].second >= patM[0].second) {         long long fin = matches[i] + patM.size() - 2;         long long le = patM.size() - 1;         if (fin < txtM.size() && txtM[fin].first == patM[le].first &&             txtM[fin].second >= patM[le].second) {           cnt += 1;         }       }     }     cout << cnt << endl;     return 0;   }