#include <bits/stdc++.h>
using namespace std;
string text;
int b[4000006];
int p[4000006];
int t[4000006];
int ok[4000006];
pair<long long, char> pat[4000006], txt[4000006], mid[4000006];
int n, m;
void kmp_preprocessing() {
  int i, j;
  i = 0;
  j = -1;
  b[i] = j;
  int sz = m - 2;
  while (i < sz) {
    while (j >= 0 && mid[i] != mid[j]) j = b[j];
    i++;
    j++;
    b[i] = j;
  }
}
int ans = 0;
int kmp_search() {
  int i = 0, j = 0;
  while ((i + j) < n) {
    if (mid[j] == txt[i + j]) {
      if (j == m - 3) {
        ok[i + j] = 1;
        i = i + j - b[j];
        if (b[j] >= 0)
          j = b[j];
        else
          j = 0;
      } else
        j++;
    } else {
      i = i + j - b[j];
      if (b[j] >= 0)
        j = b[j];
      else
        j = 0;
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i <= n - 1; i++) {
    char l, ch, c;
    cin >> txt[i].first >> ch >> txt[i].second;
    if (i && txt[i].second == txt[i - 1].second) {
      txt[i - 1].first += txt[i].first;
      i--;
      n--;
    }
  }
  for (int i = 0; i <= m - 1; i++) {
    char l, ch, c;
    cin >> pat[i].first >> ch >> pat[i].second;
    if (i && pat[i].second == pat[i - 1].second) {
      pat[i - 1].first += pat[i].first;
      i--;
      m--;
    }
  }
  if (m == 1) {
    long long res = 0;
    for (int i = 0; i < n; i++) {
      if (pat[0].second == txt[i].second) {
        res += max(0LL, txt[i].first - pat[0].first + 1LL);
      }
    }
    cout << res << endl;
    return 0;
  } else if (m == 2) {
    long long res = 0;
    for (int i = 0; i < n - 1; i++) {
      int j = i + 1;
      if (pat[0].second != txt[i].second) continue;
      if (pat[1].second != txt[j].second) continue;
      if (pat[0].first > txt[i].first) continue;
      if (pat[1].first > txt[j].first) continue;
      res++;
    }
    cout << res << endl;
    return 0;
  } else {
    int j = 0;
    for (int i = 1; i < m - 1; i++) {
      mid[j++] = pat[i];
    }
    kmp_preprocessing();
    kmp_search();
    long long res = 0;
    for (int i = 0; i < n; i++) {
      if (ok[i]) {
        int l, r;
        l = i - (m - 2);
        r = i + 1;
        if (r >= n) continue;
        if (l < 0) continue;
        if (txt[l].first >= pat[0].first && txt[l].second == pat[0].second &&
            txt[r].first >= pat[m - 1].first &&
            txt[r].second == pat[m - 1].second)
          res++;
      }
    }
    cout << res << endl;
  }
  return 0;
}
