#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
const int maxlen = 1000000 + 10;
int n, m, a[maxn], b[maxn], fail[maxn];
long long ans, numa[maxn], numb[maxn];
void getfail() {
  fail[1] = 1;
  fail[2] = 1;
  for (int i = 2; i <= m; ++i) {
    int j = fail[i];
    while (j > 1 && b[i] != b[j] && numb[i] != numb[j]) j = fail[j];
    fail[i + 1] = (b[i] == b[j] && numb[i] == numb[i]) ? j + 1 : 1;
  }
}
void find() {
  int j = 1;
  for (int i = 1; i <= n; ++i) {
    while (j > 1 &&
           (b[j] != a[i] || ((j > 1 && j < m && numb[j] != numa[i]) ||
                             ((j == 1 || j == m) && numb[j] > numa[i]))))
      j = fail[j];
    if (b[j] == a[i] &&
        (numb[j] == numa[i] || (j == 1 || j == m) && numb[j] <= numa[i]))
      j++;
    if (j > m) ans++, j = fail[j];
  }
}
void init() {
  char ch;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d-%c", &numa[i], &ch);
    a[i] = ch - 'a' + 1;
  }
  for (int j = 1; j <= m; ++j) {
    scanf("%d-%c", &numb[j], &ch);
    b[j] = ch - 'a' + 1;
  }
  int tota = 0, totb = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != a[tota] || tota == 0)
      a[++tota] = a[i], numa[tota] = numa[i];
    else
      numa[tota] += numa[i];
  }
  for (int i = 1; i <= m; ++i) {
    if (b[i] != b[totb] || totb == 0)
      b[++totb] = b[i], numb[totb] = numb[i];
    else
      numb[totb] += numb[i];
  }
  n = tota;
  m = totb;
  getfail();
}
void solve() {
  if (m == 1) {
    for (int i = 1; i <= n; ++i)
      if (a[i] == b[1] && numa[i] >= numb[1]) ans += numa[i] - numb[1] + 1;
  } else
    find();
  printf("%I64d\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}
