#include <bits/stdc++.h>
using namespace std;
long long ct[200010], cs[200010];
int vt[200010], vs[200010];
long long res[2 * 200010];
int z[2 * 200010];
void Zval(long long *s, int n) {
  z[0] = 0;
  for (int b = 0, i = 1; i < n; i++) {
    z[i] = max(min(z[i - b], z[b] + b - i), 0);
    while (s[i + z[i]] == s[z[i]]) z[i]++;
    if (i + z[i] > b + z[b]) b = i;
  }
}
int nn;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  long long L1 = 0;
  for (int i = 1; i <= n; i++) {
    char ss, st;
    scanf("%I64d %c %c", &ct[i], &ss, &st);
    vt[i] = st - 'a';
    L1 += ct[i];
  }
  long long L2 = 0;
  for (int i = 1; i <= m; i++) {
    char ss, st;
    scanf("%I64d %c %c", &cs[i], &ss, &st);
    vs[i] = st - 'a';
    L2 += cs[i];
  }
  if (n < m || L1 < L2) {
    puts("0");
    return 0;
  }
  nn = 0;
  for (int i = 1; i <= n; i++) {
    nn++;
    int id = i;
    long long cnt = 0;
    while (vt[id] == vt[i] && id <= n) {
      cnt += ct[id];
      id++;
    }
    id--;
    vt[nn] = vt[i];
    ct[nn] = cnt;
    i = id;
  }
  n = nn;
  nn = 0;
  for (int i = 1; i <= m; i++) {
    nn++;
    int id = i;
    long long cnt = 0;
    while (vs[id] == vs[i] && id <= m) {
      cnt += cs[id];
      id++;
    }
    id--;
    vs[nn] = vs[i];
    cs[nn] = cnt;
    i = id;
  }
  m = nn;
  if (m > n) {
    puts("0");
    return 0;
  }
  long long ans = 0;
  if (m == 1) {
    for (int i = 1; i <= n; i++) {
      if (vt[i] == vs[1] && ct[i] >= cs[1]) ans += ct[i] - cs[1] + 1;
    }
    printf("%I64d\n", ans);
    return 0;
  }
  if (m == 2) {
    for (int i = 1; i < n; i++) {
      if (vt[i] == vs[1] && vt[i + 1] == vs[2] && ct[i] >= cs[1] &&
          ct[i + 1] >= cs[2])
        ans++;
    }
    printf("%I64d\n", ans);
    return 0;
  }
  int len = 0;
  for (int i = 2; i < m; i++) res[len++] = vs[i] + 1000 * cs[i];
  for (int i = 1; i <= n; i++) res[len++] = vt[i] + 1000 * ct[i];
  Zval(res, len);
  for (int i = 2; i <= n - m + 2; i++) {
    int Pos = i + m - 3;
    if (z[Pos] < m - 2) continue;
    if (vt[i - 1] != vs[1] || vt[i + m - 2] != vs[m]) continue;
    if (ct[i - 1] < cs[1] || ct[i + m - 2] < cs[m]) continue;
    ans++;
  }
  printf("%I64d\n", ans);
  return 0;
}
