#include <bits/stdc++.h>
using namespace std;
const long long maxn = 400010;
long long n, m, cnt, ans, nextt[maxn], flag[maxn];
struct node {
  long long x, num;
} a[maxn], b[maxn], t[maxn], c[maxn];
void solve1() {
  if (m == 1) {
    for (long long i = 1; i <= n; i++)
      if (a[i].x == b[1].x) ans += max(0LL, a[i].num - b[1].num + 1);
  } else {
    for (long long i = 1; i < n; i++)
      if (a[i].x == b[1].x && a[i + 1].x == b[2].x && a[i].num >= b[1].num &&
          a[i + 1].num >= b[2].num)
        ans++;
  }
  printf("%lld\n", ans);
}
void solve2() {
  cnt = 0;
  for (long long i = 2; i <= m - 1; i++) c[++cnt] = b[i];
  long long j = 0;
  for (long long i = 2; i <= cnt; i++) {
    while (j && (c[j + 1].x != c[i].x || c[j + 1].num != c[i].num))
      j = nextt[j];
    if (c[j + 1].x == c[i].x && c[j + 1].num == c[i].num) j++;
    nextt[i] = j;
  }
  j = 0;
  for (long long i = 1; i <= n; i++) {
    while (j && (c[j + 1].x != a[i].x || c[j + 1].num != a[i].num))
      j = nextt[j];
    if (c[j + 1].x == a[i].x && c[j + 1].num == a[i].num) j++;
    if (j >= cnt) flag[i] = 1;
  }
  for (long long i = 1; i <= n; i++)
    if (flag[i]) {
      long long l = i - cnt, r = i + 1;
      if (l < 1 || r > n) continue;
      if (a[l].x == b[1].x && a[l].num >= b[1].num && a[r].x == b[m].x &&
          a[r].num >= b[m].num)
        ans++;
    }
  printf("%lld\n", ans);
}
int main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    long long x;
    char ch;
    scanf("%lld-%c", &x, &ch);
    t[i].num = x;
    t[i].x = ch - 'a' + 1;
  }
  for (long long i = 1; i <= n; i++) {
    if (t[i].x == t[i + 1].x)
      t[i + 1].num += t[i].num;
    else
      a[++cnt] = t[i];
  }
  for (int i = 1; i <= 200000; i++) t[i].x = 0;
  n = cnt;
  cnt = 0;
  for (long long i = 1; i <= m; i++) {
    long long x;
    char ch;
    scanf("%lld-%c", &x, &ch);
    t[i].num = x;
    t[i].x = ch - 'a' + 1;
  }
  for (long long i = 1; i <= m; i++) {
    if (t[i].x == t[i + 1].x)
      t[i + 1].num += t[i].num;
    else
      b[++cnt] = t[i];
  }
  m = cnt;
  if (m <= 2)
    solve1();
  else
    solve2();
  return 0;
}
