#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<long long, char> as[666666], bs[666666];
void m_1() {
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (as[i].second == bs[1].second && as[i].first >= bs[1].first)
      ans += as[i].first - bs[1].first + 1;
  }
  printf("%I64d\n", ans);
}
void m_2() {
  long long ans = 0;
  for (int i = 1; i + 1 <= n; i++) {
    if (as[i].second == bs[1].second && as[i].first >= bs[1].first &&
        as[i + 1].second == bs[2].second && as[i + 1].first >= bs[2].first)
      ++ans;
  }
  printf("%I64d\n", ans);
}
int ms = 0, ml[666666], mr[666666];
struct HashKMP {
  pair<long long, char> s[666666 + 1];
  int n, next[666666 + 3];
  void gnext() {
    n = 0;
    while (s[n].second) ++n;
    next[0] = -1;
    int j = -1;
    for (int i = 1; i < n; i++) {
      while (j != -1 && s[i].second != s[j + 1].second) j = next[j];
      if (s[i].second == s[j + 1].second) ++j;
      next[i] = j;
    }
  }
  void kmp(pair<long long, char>* a) {
    int j = -1;
    for (int i = 0; a[i].second; i++) {
      while (j != -1 && s[j + 1] != a[i]) j = next[j];
      if (s[j + 1] == a[i]) ++j;
      if (j == n - 1) ++ms, ml[ms] = i - n + 1, mr[ms] = i + 2;
    }
  }
} ha;
pair<long long, char> hb[666666];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int a;
    char b[3];
    scanf("%d-%s", &a, b);
    if (i > 1 && as[i - 1].second == b[0]) {
      as[i - 1].first += a;
      --i;
      --n;
      continue;
    }
    as[i] = pair<long long, char>(a, b[0]);
  }
  for (int i = 1; i <= m; i++) {
    int a;
    char b[3];
    scanf("%d-%s", &a, b);
    if (i > 1 && bs[i - 1].second == b[0]) {
      bs[i - 1].first += a;
      --i;
      --m;
      continue;
    }
    bs[i] = pair<long long, char>(a, b[0]);
  }
  if (m == 1) {
    m_1();
    return 0;
  }
  if (m == 2) {
    m_2();
    return 0;
  }
  for (int i = 2; i <= m - 1; i++) ha.s[i - 2] = bs[i];
  ha.gnext();
  for (int i = 1; i <= n; i++) hb[i - 1] = as[i];
  ha.kmp(hb);
  long long ans = 0;
  for (int i = 1; i <= ms; i++) {
    int a = ml[i], b = mr[i];
    if (a < 1 || b < 1 || a > n || b > n) continue;
    if (as[a].second == bs[1].second && as[a].first >= bs[1].first)
      ;
    else
      continue;
    if (as[b].second == bs[m].second && as[b].first >= bs[m].first)
      ;
    else
      continue;
    ++ans;
  }
  printf("%I64d\n", ans);
}
