#include <bits/stdc++.h>
using namespace std;
int n, m;
long long int numa[210000];
char cha[210000];
int tota = 0;
long long int numb[210000];
char chb[210000];
int totb = 0;
int nxt[210000];
namespace nGeq3 {
void getnxt() {
  int p = 1;
  nxt[2] = 1;
  for (int i = 3; i < totb; i++) {
    while (p > 1 && (numb[p + 1] != numb[i] || chb[p + 1] != chb[i]))
      p = nxt[p];
    if (numb[p + 1] == numb[i] && chb[p + 1] == chb[i]) p++;
    nxt[i] = p;
  }
}
void solve() {
  getnxt();
  int ans = 0;
  int p = 1;
  for (int i = 1; i <= tota; i++) {
    while (p > 1 &&
           (numb[p + 1] != numa[i] || chb[p + 1] != cha[i] || p + 1 == totb)) {
      p = nxt[p];
    }
    if (p + 1 < totb && numb[p + 1] == numa[i] && chb[p + 1] == cha[i]) p++;
    if (p == totb - 1) {
      int l = 0, r = 0;
      if (numa[i - (totb - 2)] >= numb[1] && cha[i - (totb - 2)] == chb[1])
        l = 1;
      if (numa[i + 1] >= numb[totb] && cha[i + 1] == chb[totb]) r = 1;
      ans += l * r;
      p = nxt[p];
    }
  }
  printf("%d\n", ans);
}
}  // namespace nGeq3
namespace nequal2 {
void solve() {
  int ans = 0;
  for (int i = 1; i + totb - 1 <= tota; i++) {
    bool flag = true;
    for (int j = 1; j <= totb; j++) {
      if (numa[i + j - 1] < numb[j] || cha[i + j - 1] != chb[j]) {
        flag = false;
        break;
      }
    }
    if (flag) ans++;
  }
  printf("%d\n", ans);
}
}  // namespace nequal2
namespace nequal1 {
void solve() {
  long long int ans = 0;
  for (int i = 1; i <= tota; i++) {
    if (cha[i] == chb[1]) ans += max(0LL, numa[i] - numb[1] + 1);
  }
  printf("%I64d\n", ans);
}
}  // namespace nequal1
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    long long int x;
    char s[10];
    scanf("%I64d", &x);
    scanf("%s", s);
    if (s[1] == cha[tota])
      numa[tota] += x;
    else {
      tota++;
      numa[tota] = x;
      cha[tota] = s[1];
    }
  }
  for (int i = 1; i <= m; i++) {
    long long int x;
    char s[10];
    scanf("%I64d", &x);
    scanf("%s", s);
    if (s[1] == chb[totb])
      numb[totb] += x;
    else {
      totb++;
      numb[totb] = x;
      chb[totb] = s[1];
    }
  }
  if (totb >= 3)
    nGeq3::solve();
  else if (totb == 2)
    nequal2::solve();
  else
    nequal1::solve();
  return 0;
}
