#include <bits/stdc++.h>
using namespace std;
struct RTC {};
int n, m;
vector<pair<char, long long int> > comprimir(
    vector<pair<char, long long int> > vec) {
  vector<pair<char, long long int> > ans(1, vec[0]);
  for (int i = 1; i < ((int)(vec).size()); i++)
    if (vec[i].first == ans.back().first)
      ans[((int)(ans).size()) - 1].second += vec[i].second;
    else
      ans.push_back(vec[i]);
  return ans;
}
vector<int> KMP(vector<pair<char, long long int> > S,
                vector<pair<char, long long int> > K) {
  vector<int> T(K.size() + 1, -1);
  for (int i = 1; i <= (int)K.size(); i++) {
    int pos = T[i - 1];
    while (pos != -1 && K[pos] != K[i - 1]) pos = T[pos];
    T[i] = pos + 1;
  }
  vector<int> matches;
  for (int sp = 0, kp = 0; sp < (int)S.size(); sp++) {
    while (kp != -1 && (kp == (int)K.size() || K[kp] != S[sp])) kp = T[kp];
    kp++;
    if (kp == (int)K.size()) matches.push_back(sp + 1 - K.size());
  }
  return matches;
}
int main() {
  scanf("%d %d", &n, &m);
  long long int szT = 0, szS = 0;
  vector<pair<char, long long int> > T, S;
  for (int i = 0; i < (n); i++) {
    int tam;
    char c;
    scanf("%d-%c", &tam, &c);
    T.emplace_back(c, tam);
    szT += tam;
  }
  for (int i = 0; i < (m); i++) {
    int tam;
    char c;
    scanf("%d-%c", &tam, &c);
    S.emplace_back(c, tam);
    szS += tam;
  }
  S = comprimir(S);
  T = comprimir(T);
  if (szS > szT) {
    printf("0\n");
    return 0;
  }
  long long int ans = 0;
  if (((int)(S).size()) == 1) {
    char first = S[0].first;
    long long int second = S[0].second;
    for (pair<char, long long int> e : T)
      if (e.first == first && e.second >= second)
        ans += (e.second - second + 1LL);
  } else if (((int)(S).size()) == 2) {
    char letraA = S[0].first, letraB = S[1].first;
    long long int cantA = S[0].second, cantB = S[1].second;
    for (int i = 0; i < ((int)(T).size()) - 1; i++) {
      if (T[i].first == letraA && T[i + 1].first == letraB &&
          T[i].second >= cantA && T[i + 1].second >= cantB)
        ans++;
    }
  } else {
    vector<pair<char, long long int> > p;
    for (int i = 1; i < ((int)(S).size()) - 1; i++) p.push_back(S[i]);
    vector<int> pos = KMP(T, p);
    char letraA = S[0].first, letraB = S.back().first;
    long long int cantA = S[0].second, cantB = S.back().second;
    for (int i : pos) {
      if ((i - 1) >= 0) {
        if (T[i - 1].first == letraA && T[i - 1].second >= cantA) {
          int j = (i - 1) + ((int)(S).size()) - 1;
          if (j < ((int)(T).size()) && T[j].first == letraB &&
              T[j].second >= cantB) {
            ans++;
          }
        }
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
