#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200007;
int n, m;
long long ints1[MAXN], ints2[MAXN];
char s1[MAXN], s2[MAXN];
char S[MAXN], T[MAXN];
long long intT[MAXN];
int nxt[MAXN];
void kmp_pre(char s[]) {
  int i, j;
  memset(nxt, -1, sizeof(nxt));
  j = nxt[0] = -1;
  i = 0;
  int len = strlen(s);
  while (i < len) {
    while (j != -1 && (s[i] != s[j] || intT[i] != intT[j])) j = nxt[j];
    if (s[++i] == s[++j] && intT[i] == intT[j])
      nxt[i] = nxt[j];
    else
      nxt[i] = j;
  }
}
long long kmp(char s[], char t[]) {
  int i, j;
  int ls = strlen(s), lt = strlen(t);
  kmp_pre(t);
  i = j = 0;
  long long ret = 0;
  while (i < ls) {
    while (-1 != j && (s[i] != t[j] || ints1[i] != intT[j])) j = nxt[j];
    i++, j++;
    if (j >= lt) {
      if (i - lt - 1 >= 0 && i < ls && s1[i - lt - 1] == s2[0] &&
          s1[i] == s2[lt + 1] && ints1[i - lt - 1] >= ints2[0] &&
          ints1[i] >= ints2[lt + 1]) {
        ret++;
      }
      j = nxt[j];
    }
  }
  return ret;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    long long num, pre = 0, cnt = 0;
    char ch;
    char s[12];
    for (int i = 0; i < n; i++) {
      scanf("%lld", &num);
      scanf("%s", s);
      if (i == 0) {
        pre += num;
        ch = s[1];
      } else if (ch == s[1]) {
        pre += num;
      } else {
        s1[cnt] = ch;
        ints1[cnt++] = pre;
        pre = num;
        ch = s[1];
      }
    }
    s1[cnt] = ch;
    ints1[cnt++] = pre;
    s1[cnt] = '\0';
    pre = 0;
    cnt = 0;
    for (int i = 0; i < m; i++) {
      scanf("%lld", &num);
      scanf("%s", s);
      if (i == 0) {
        ch = s[1];
        pre += num;
      } else if (ch == s[1]) {
        pre += num;
      } else {
        s2[cnt] = ch;
        ints2[cnt++] = pre;
        pre = num;
        ch = s[1];
      }
    }
    s2[cnt] = ch;
    ints2[cnt++] = pre;
    s2[cnt] = '\0';
    pre = 0;
    cnt = 0;
    int len1 = strlen(s1), len2 = strlen(s2);
    long long ans = 0;
    if (len2 == 1) {
      for (int i = 0; i < len1; i++) {
        if (s1[i] == s2[0] && ints1[i] >= ints2[0]) {
          long long tmp1 = ints1[i], tmp2 = ints2[0];
          ans = ans + tmp1 - tmp2 + 1;
        }
      }
    } else if (len2 == 2) {
      for (int i = 0; i + 1 < len1; i++) {
        if ((s1[i] == s2[0]) && (s1[i + 1] == s2[1]) &&
            (ints1[i] >= ints2[0]) && (ints1[i + 1] >= ints2[1]))
          ans++;
      }
    } else {
      strcpy(T, s2 + 1);
      T[strlen(T) - 1] = '\0';
      for (int i = 0; i + 1 < len2 - 1; i++) {
        intT[i] = ints2[i + 1];
      }
      ans = kmp(s1, T);
    }
    cout << ans << endl;
  }
  return 0;
}
