#include <bits/stdc++.h>
using namespace std;
long long up, l1, l2, tot, na[1000500], nb[1000500], ca[1000500], cb[1000500],
    Ans;
long long ha[1000500], hb[1000500], fac[1000500];
char s[105];
long long gha(long long l, long long r) {
  return (ha[r] - ha[l - 1] * fac[r - l + 1] % 1000000009 + 1000000009) %
         1000000009;
}
long long ghb(long long l, long long r) {
  return (hb[r] - hb[l - 1] * fac[r - l + 1] % 1000000009 + 1000000009) %
         1000000009;
}
signed main() {
  scanf("%lld%lld", &l1, &l2);
  for (long long i = 1; i <= l1; i++) {
    scanf("%s", s);
    na[++tot] = 0;
    for (long long j = 0; s[j] >= '0' && s[j] <= '9'; j++) {
      na[tot] *= 10;
      na[tot] += s[j] - '0';
    }
    ca[tot] = s[strlen(s) - 1] - 'a' + 1;
    if (ca[tot] == ca[tot - 1]) {
      tot--;
      na[tot] += na[tot + 1];
    }
  }
  l1 = tot;
  tot = 0;
  for (long long i = 1; i <= l2; i++) {
    scanf("%s", s);
    nb[++tot] = 0;
    for (long long j = 0; s[j] >= '0' && s[j] <= '9'; j++) {
      nb[tot] *= 10;
      nb[tot] += s[j] - '0';
    }
    cb[tot] = s[strlen(s) - 1] - 'a' + 1;
    if (cb[tot] == cb[tot - 1]) {
      tot--;
      nb[tot] += nb[tot + 1];
    }
  }
  l2 = tot;
  up = max(l1, l2);
  fac[0] = 1;
  for (long long i = 1; i <= up; i++) fac[i] = fac[i - 1] * 2333 % 1000000009;
  for (long long i = 1; i <= l1; i++)
    ha[i] = (ha[i - 1] * 2333 + na[i] + ca[i] * 19260817) % 1000000009;
  for (long long i = 1; i <= l2; i++)
    hb[i] = (hb[i - 1] * 2333 + nb[i] + cb[i] * 19260817) % 1000000009;
  if (l2 == 1) {
    for (long long i = 1; i <= l1; i++)
      if (ca[i] == cb[1] && na[i] >= nb[1]) Ans += na[i] - nb[1] + 1;
    printf("%lld\n", Ans);
    return 0;
  }
  if (l2 == 2) {
    for (long long i = 1; i < l1; i++)
      if (ca[i] == cb[1] && ca[i + 1] == cb[2] && na[i] >= nb[1] &&
          na[i + 1] >= nb[2])
        Ans++;
    printf("%lld\n", Ans);
    return 0;
  }
  for (long long i = 1; i + l2 - 1 <= l1; i++)
    if (ca[i] == cb[1] && ca[i + l2 - 1] == cb[l2] && na[i] >= nb[1] &&
        na[i + l2 - 1] >= nb[l2] && gha(i + 1, i + l2 - 2) == ghb(2, l2 - 1))
      Ans++;
  printf("%lld\n", Ans);
  return 0;
}
