#include <bits/stdc++.h>
const int maxn = 2e5 + 10;
char s[maxn], c[maxn], mid[maxn];
int f[maxn];
long long ans = 0, frist[maxn], second[maxn];
void getfill() {
  f[0] = f[1] = 0;
  int len = strlen(c);
  for (int i = 1; i < len; i++) {
    int j = f[i];
    while (j && (c[i] != c[j] || second[i + 1] != second[j + 1])) j = f[j];
    f[i + 1] = (c[i] == c[j] && second[i + 1] == second[j + 1]) ? j + 1 : 0;
  }
}
long long finds() {
  int j = 0;
  ans = 0;
  getfill();
  int len = strlen(s);
  int lenc = strlen(c);
  for (int i = 0; i < len; i++) {
    while (j && (s[i] != c[j] || frist[i] != second[j + 1])) j = f[j];
    if (s[i] == c[j] && frist[i] == second[j + 1]) j++;
    if (j == lenc) {
      if (s[i + 1] == mid[j + 1] && frist[i + 1] >= second[j + 1] &&
          s[i - lenc] == mid[j - lenc] && frist[i - lenc] >= second[j - lenc]) {
        ans++;
      }
      j = f[j];
    }
  }
  return ans;
}
int main() {
  int n, m, coa = 0, cos = 0, ih;
  int coi = 0, coj = 0;
  char ch;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d-%c", &ih, &ch);
    if (ch == s[cos - 1]) {
      frist[coi - 1] = frist[coi - 1] + ih;
    } else {
      frist[coi++] = ih;
      s[cos++] = ch;
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d-%c", &ih, &ch);
    if (ch == mid[coa - 1]) {
      second[coj - 1] = second[coj - 1] + ih;
    } else {
      second[coj++] = ih;
      mid[coa++] = ch;
    }
  }
  if (coa == 1) {
    for (int i = 0; i < cos; i++) {
      if (mid[0] == s[i] && second[0] <= frist[i]) {
        long long l = frist[i] - second[0] + 1;
        ans += l;
      }
    }
  } else if (coa == 2) {
    for (int i = 0; i < cos - 1; i++) {
      if (mid[0] == s[i] && mid[1] == s[i + 1] && second[0] <= frist[i] &&
          second[1] <= frist[i + 1]) {
        ans++;
      }
    }
  } else {
    for (int i = 0; i < coa - 2; i++) c[i] = mid[i + 1];
    getfill();
    ans = finds();
  }
  printf("%I64d\n", ans);
  return 0;
}
