#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, m;
char s[20];
int Z[maxn];
vector<pair<long long, int> > a, b, c;
void Zfunc() {
  int l = -1, r = -1;
  Z[0] = c.size();
  for (int i = 1; i < c.size(); i++) {
    if (i <= r) Z[i] = min(Z[i - l], r - i + 1);
    while (i + Z[i] < c.size() && c[i + Z[i]] == c[Z[i]]) Z[i]++;
    if (i + Z[i] - 1 > r) {
      r = i + Z[i] - 1;
      l = i;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    long long l;
    scanf("%I64d", &l);
    scanf("%s", s);
    int v = s[1] - 'a';
    if (a.empty() || a.back().second != v)
      a.push_back(make_pair(l, v));
    else
      a.back().first += l;
  }
  for (int i = 0; i < m; i++) {
    long long l;
    scanf("%d", &l);
    scanf("%s", s);
    int v = s[1] - 'a';
    if (b.empty() || b.back().second != v)
      b.push_back(make_pair(l, v));
    else
      b.back().first += l;
  }
  n = a.size();
  m = b.size();
  if (m == 1) {
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      if (a[i].second == b[0].second && a[i].first >= b[0].first) {
        ans += a[i].first - b[0].first + 1;
      }
    }
    printf("%I64d\n", ans);
    return 0;
  }
  if (m == 2) {
    long long ans = 0;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].second == b[0].second && a[i + 1].second == b[1].second &&
          a[i].first >= b[0].first && a[i + 1].first >= b[1].first)
        ans++;
    }
    printf("%I64d\n", ans);
    return 0;
  }
  for (int i = 1; i + 1 < m; i++) c.push_back(b[i]);
  c.push_back(make_pair(-1, -1));
  for (int i = 0; i < n; i++) c.push_back(a[i]);
  Zfunc();
  long long ans = 0;
  for (int i = m - 1; i < c.size(); i++) {
    int j = i - m + 1;
    if (Z[i] == m - 2) {
      if (j && j + m - 2 < n && a[j - 1].second == b[0].second &&
          a[j - 1].first >= b[0].first &&
          a[j + m - 2].second == b[m - 1].second &&
          a[j + m - 2].first >= b[m - 1].first) {
        ans++;
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
