#include <bits/stdc++.h>
using namespace std;
long long int a[200005], b[200005], ans = 0;
char txt[200005], v, pat[200005];
void searchi(long long int m, long long int n, long long int* lps) {
  long long int j = 0, i = 0, N = n, M = m;
  while (i < N) {
    if ((pat[j] == txt[i]) &&
        (b[j] == a[i] || (b[j] < a[i] && (j == 0 || j == m - 1))))
      j++, i++;
    if (j == m) {
      ans++;
      j = lps[j - 1];
    } else if (i < N &&
               ((pat[j] != txt[i]) || (a[i] != b[j] && j > 0 && j < m - 1) ||
                (a[i] < b[j] && (j == 0 || j == m - 1)))) {
      if (j != 0)
        j = lps[j - 1];
      else
        i++;
    }
  }
}
void findlpsi(long long int M, long long int* lps) {
  long long int len = 0, i = 1;
  lps[0] = 0;
  i = 1;
  while (i < M)
    if (pat[i] == pat[len] && a[len] == b[i])
      lps[i++] = ++len;
    else if (len != 0)
      len = lps[len - 1];
    else
      lps[i++] = 0;
}
int main() {
  long long int i, j, k, l, n, m, lps[200005], l1 = 0, l2 = 0;
  cin >> n >> m;
  for (i = 0; i < n; i++) cin >> a[i] >> v >> txt[i];
  for (i = 0; i < m; i++) cin >> b[i] >> v >> pat[i];
  for (i = 0; i < n; i++) {
    txt[l1] = txt[i];
    a[l1] = a[i];
    while (i < n - 1 && txt[i] == txt[i + 1]) a[l1] += a[++i];
    l1++;
  }
  for (i = 0; i < m; i++) {
    pat[l2] = pat[i];
    b[l2] = b[i];
    while (i < m - 1 && pat[i] == pat[i + 1]) b[l2] += b[++i];
    l2++;
  }
  n = l1;
  m = l2;
  long long int f = 0;
  for (i = 0; i < n; i++) {
    if (m == 1) {
      if (txt[i] == pat[0]) {
        long long int sum = a[i];
        while (i < n - 1 && txt[i] == txt[i + 1]) {
          sum += a[i + 1];
          i++;
        }
        ans += max(0ll, sum - b[0] + 1);
      }
    } else {
      if (!f) {
        findlpsi(m, lps);
        searchi(m, n, lps);
      }
      f = 1;
    }
  }
  cout << ans << endl;
  return 0;
}
