#include <bits/stdc++.h>
int n, m, N, M;
char S[200005], T[200005];
long long Sn[200005], Tn[200005];
int pi[200005];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    char y;
    scanf("%d-%c", &x, &y);
    if (N && S[N] == y)
      Sn[N] += x;
    else
      S[++N] = y, Sn[N] = x;
  }
  for (int i = 1; i <= m; i++) {
    int x;
    char y;
    scanf("%d-%c", &x, &y);
    if (M && T[M] == y)
      Tn[M] += x;
    else
      T[++M] = y, Tn[M] = x;
  }
  long long res = 0;
  if (M == 1) {
    for (int i = 1; i <= N; i++)
      if (T[1] == S[i] && Sn[i] >= Tn[1]) res += Sn[i] - Tn[1] + 1;
  } else {
    int p = 1;
    pi[1] = pi[2] = 1;
    for (int i = 3; i < M; i++) {
      while (p > 1 && (T[p + 1] != T[i] || Tn[p + 1] != Tn[i])) p = pi[p];
      if (T[p + 1] == T[i] && Tn[p + 1] == Tn[i]) p++;
      pi[i] = p;
    }
    p = 1;
    for (int i = 1; i < N; i++) {
      while (p > 1 && (T[p + 1] != S[i] || Tn[p + 1] != Sn[i])) p = pi[p];
      if (T[p + 1] == S[i] && Tn[p + 1] == Sn[i]) p++;
      if (p == M - 1) {
        if (S[i + 1] == T[M] && Sn[i + 1] >= Tn[M] && S[i - M + 2] == T[1] &&
            Sn[i - M + 2] >= Tn[1])
          res++;
        p = pi[p];
      }
    }
  }
  printf("%I64d", res);
}
