#include <bits/stdc++.h>
using namespace std;
const unsigned long long base = 1e9 + 7;
const int maxn = 2e5 + 5;
int n, m;
struct NODE {
  long long l;
  char c;
} A[maxn + 5], B[maxn + 5];
void Debug() {
  for (int i = 1; i <= n; i++) printf("%lld-%c ", A[i].l, A[i].c);
  printf("\n");
  for (int i = 1; i <= m; i++) printf("%lld-%c ", B[i].l, B[i].c);
  printf("\n");
}
void Init() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%lld-%c", &A[i].l, &A[i].c);
    if (A[i].c == A[i - 1].c) {
      A[i - 1].l += A[i].l;
      n--;
      i--;
    }
  }
  for (int i = 1; i <= m; i++) {
    scanf("%lld-%c", &B[i].l, &B[i].c);
    if (B[i].c == B[i - 1].c) {
      B[i - 1].l += B[i].l;
      m--;
      i--;
    }
  }
}
unsigned long long bit[maxn + 5];
unsigned long long hashA[maxn + 5];
unsigned long long hashB[maxn + 5];
void Hash() {
  bit[0] = 1ULL;
  for (int i = 1; i <= maxn; i++) bit[i] = bit[i - 1] * base;
  for (int i = 1; i <= n; i++)
    hashA[i] = hashA[i - 1] * base + 100ULL * A[i].l + 1ULL * (A[i].c - 'a');
  for (int i = 1; i <= m; i++)
    hashB[i] = hashB[i - 1] * base + 100ULL * B[i].l + 1ULL * (B[i].c - 'a');
}
unsigned long long GetHash(int L, int R, int flag) {
  if (flag == 1)
    return hashA[R] - hashA[L - 1] * bit[R - L + 1];
  else if (flag == 2)
    return hashB[R] - hashB[L - 1] * bit[R - L + 1];
}
void Solve() {
  long long ans = 0LL;
  if (m == 1) {
    for (int i = 1; i <= n; i++) {
      if (A[i].c == B[1].c && A[i].l >= B[1].l) {
        ans = ans + A[i].l - B[1].l + 1LL;
      }
    }
  } else if (m == 2) {
    for (int i = 2; i <= n; i++) {
      if (A[i - 1].c == B[1].c && A[i].c == B[2].c) {
        if (A[i - 1].l >= B[1].l && A[i].l >= B[2].l) {
          ans = ans + 1LL;
        }
      }
    }
  } else if (m >= 3) {
    Hash();
    unsigned long long a, b;
    b = GetHash(2, m - 1, 2);
    for (int i = m; i <= n; i++) {
      if (A[i - m + 1].c == B[1].c && A[i].c == B[m].c) {
        if (A[i - m + 1].l >= B[1].l && A[i].l >= B[m].l) {
          a = GetHash(i - m + 2, i - 1, 1);
          if (a == b) ans++;
        }
      }
    }
  }
  printf("%lld\n", ans);
}
int main() {
  Init();
  Solve();
  return 0;
}
