#include <bits/stdc++.h>
using namespace std;
const int maxn = 200505;
const int mod = (int)1e9 + 7;
long long numfa[maxn], numson[maxn], numtmp[maxn];
char aimfa[maxn], aimson[maxn], aimtmp[maxn];
int nex[maxn], n, m;
vector<int> preans;
void kmp_next(char *str2, long long *num, int *next, int len) {
  int i, j;
  next[1] = 0;
  j = 0;
  for (int i = 2; i <= len; i++) {
    while (j > 0 && (str2[j + 1] != str2[i] || num[j + 1] != num[i]))
      j = next[j];
    if (str2[j + 1] == str2[i] && num[j + 1] == num[i]) j++;
    next[i] = j;
  }
}
int kmp_re(char *str1, long long *num1, char *str2, long long *num2, int *next,
           int len1, int len2) {
  int j = 0;
  for (int i = 1; i <= len1; i++) {
    while (j > 0 && (str2[j + 1] != str1[i] || num2[j + 1] != num1[i]))
      j = next[j];
    if (str2[j + 1] == str1[i] && num2[j + 1] == num1[i]) j++;
    if (j == len2) {
      preans.push_back(i - len2);
      j = next[j];
    }
  }
}
char s[10];
int main() {
  long long ans = 0, c;
  scanf("%d%d", &n, &m);
  int gen = 0, gem = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld-%s", &c, s + 1);
    if (s[1] == aimfa[gen]) {
      numfa[gen] += c;
    } else {
      gen++;
      numfa[gen] = c;
      aimfa[gen] = s[1];
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%lld-%s", &c, s + 1);
    if (s[1] == aimson[gem]) {
      numson[gem] += c;
    } else {
      gem++;
      numson[gem] = c;
      aimson[gem] = s[1];
    }
  }
  n = gen;
  m = gem;
  if (m == 1) {
    for (int i = 1; i <= n; i++) {
      if (aimfa[i] == aimson[1]) {
        ans = (ans + max(0ll, numfa[i] - numson[1] + 1));
      }
    }
  } else if (m == 2) {
    for (int i = 1; i < n; i++) {
      if (aimson[1] == aimfa[i] && aimson[2] == aimfa[i + 1] &&
          numson[1] <= numfa[i] && numson[2] <= numfa[i + 1]) {
        ans++;
      }
    }
  } else {
    for (int i = 2; i < m; i++) {
      aimtmp[i - 1] = aimson[i];
      numtmp[i - 1] = numson[i];
    }
    kmp_next(aimtmp, numtmp, nex, m - 2);
    kmp_re(aimfa, numfa, aimtmp, numtmp, nex, n, m - 2);
    for (int i = 0; i < preans.size(); i++) {
      int l = preans[i], r = l + m - 1;
      if (aimfa[l] == aimson[1] && aimfa[r] == aimson[m] &&
          numfa[l] >= numson[1] && numfa[r] >= numson[m])
        ans++;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
