#include <bits/stdc++.h>
using namespace std;
template <class T>
void get_next(T pp[], int lenpp, int nex[]) {
  nex[1] = 1;
  for (int i = 2, j = 1; i <= lenpp; ++i)
    for (nex[i] = 1;; j = nex[j - 1])
      if (pp[i] == pp[j]) {
        nex[i] = ++j;
        break;
      } else if (j == 1)
        break;
}
template <class T>
long long kmp(T ys[], int lenys, T pp[], int lenpp, int nex[]) {
  long long ans = 0;
  for (int i = 1, j = 1; i <= lenys; ++i)
    for (;; j = nex[j - 1])
      if (j <= lenpp && ys[i] == pp[j]) {
        if (j++ == lenpp)
          if (ys[i - lenpp] >= pp[0] && ys[i + 1] >= pp[lenpp + 1]) ++ans;
        break;
      } else if (j == 1)
        break;
  return ans;
}
struct node {
  char c;
  long long k;
  node() {}
  node(char ch, long long k) : c(c), k(k) {}
  bool operator>=(const node &b) const { return c == b.c && k >= b.k; }
  bool operator==(const node &b) const { return c == b.c && k == b.k; }
};
const int __ = 200005;
node a[__], b[__];
int nex[__];
int main() {
  int n, m, idx = 0;
  scanf("%d%d", &n, &m);
  node t;
  for (int i = (1); i <= (n); i++) {
    scanf("%lld-%c", &t.k, &t.c);
    if (t.c == a[idx].c)
      a[idx].k += t.k;
    else
      a[++idx] = t;
  }
  n = idx, idx = 0;
  for (int i = (1); i <= (m); i++) {
    scanf("%lld-%c", &t.k, &t.c);
    if (t.c == b[idx].c)
      b[idx].k += t.k;
    else
      b[++idx] = t;
  }
  m = idx;
  long long ans = 0;
  if (m == 1) {
    for (int i = (1); i <= (n); i++)
      if (a[i] >= b[1]) ans += a[i].k - b[1].k + 1;
    return !printf("%lld\n", ans);
  }
  if (m == 2) {
    for (int i = (1); i <= (n - 1); i++)
      if (a[i] >= b[1] && a[i + 1] >= b[2]) ++ans;
    return !printf("%lld\n", ans);
  }
  get_next(b + 1, m - 2, nex + 1);
  printf("%lld\n", kmp(a, n, b + 1, m - 2, nex + 1));
  return 0;
}
