#include <bits/stdc++.h>
using namespace std;
const int limN = 2e5 + 5;
void hazTab(const int n, long long *second, long long *pv) {
  pv[0] = -1;
  for (int i = -1, j = 0; j < n;) {
    while (i != -1 && second[i] != second[j]) i = pv[i];
    i++, j++;
    pv[j] = i;
  }
}
void anumo(const int n, pair<long long, int> *arr, long long *ans) {
  for (int i = 0; i < n; i++) ans[i] = arr[i].first * 26 + arr[i].second;
}
void mtchs(const int N, long long *second, const int M, const long long *t,
           long long *pv, long long *ans) {
  for (int i = 0, j = 0; j < N;) {
    while (i != -1 && t[i] != second[j]) i = pv[i];
    i++, j++;
    ans[j - 1] = i;
    if (i == M) i = pv[i];
  }
}
int N = 0, M = 0, W = 0;
pair<long long, int> upo[limN], lowo[limN];
long long tb[limN], ms[limN], x[limN], y[limN], z[limN];
int main() {
  int norg, morg;
  scanf("%d%d", &norg, &morg);
  for (int i = 0, u = -1; i < norg; i++) {
    int n;
    char a;
    scanf("%d-%c", &n, &a);
    a -= 'a';
    if (a == u)
      upo[N - 1].first += n;
    else
      upo[N++] = {n, (int)a};
    u = a;
  }
  for (int i = 0, u = -1; i < morg; i++) {
    int n;
    char a;
    scanf("%d-%c", &n, &a);
    a -= 'a';
    if (a == u)
      lowo[M - 1].first += n;
    else
      lowo[M++] = {n, (int)a};
    u = a;
  }
  long long ans = 0;
  if (M == 1) {
    for (int i = 0; i < N; i++) {
      if (upo[i].second == lowo[0].second && upo[i].first >= lowo[0].first)
        ans += upo[i].first - lowo[0].first + 1;
    }
    printf("%lld\n", ans);
    return 0;
  }
  if (M == 2) {
    for (int i = 0; i + 1 < N; i++) {
      if (upo[i].second == lowo[0].second &&
          upo[i + 1].second == lowo[1].second &&
          lowo[0].first <= upo[i].first && lowo[1].first <= upo[i + 1].first)
        ans++;
    }
    printf("%lld\n", ans);
    return 0;
  }
  anumo(N, upo, x);
  anumo(M, lowo, z);
  for (int i = 1; i + 1 < N; i++) y[i - 1] = z[i];
  W = M - 2;
  hazTab(W, y, tb);
  mtchs(N, x, W, y, tb, ms);
  for (int i = M - 1; i < N; i++) {
    if (ms[i - 1] == M - 2 && upo[i - (M - 1)].second == lowo[0].second &&
        upo[i].second == lowo[M - 1].second &&
        lowo[0].first <= upo[i - (M - 1)].first &&
        lowo[M - 1].first <= upo[i].first)
      ans++;
  }
  printf("%lld\n", ans);
}
