#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n, m;
pair<long long, long long> a[300000], b[300000];
vector<pair<long long, long long> > c;
int pi[300000];
int mat[300000];
void comp(pair<long long, long long> a[], int &n) {
  int i, j;
  for (i = n - 1; i >= 1; i--) {
    if (a[i].second == a[i - 1].second)
      a[i - 1].first += a[i].first, a[i].first = 0;
  }
  n = remove_if(a, a + n,
                [](pair<long long, long long> x) { return x.first == 0; }) -
      a;
}
int main() {
  int i, j, k;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    int x;
    char y[3];
    scanf("%d-%s", &x, y);
    a[i] = pair<long long, long long>(x, y[0]);
  }
  for (i = 0; i < m; i++) {
    int x;
    char y[3];
    scanf("%d-%s", &x, y);
    b[i] = pair<long long, long long>(x, y[0]);
  }
  comp(a, n), comp(b, m);
  if (m == 1) {
    long long ans = 0;
    for (i = 0; i < n; i++) {
      if (a[i].second == b[0].second)
        ans += max(0ll, a[i].first - b[0].first + 1);
    }
    return !printf("%lld", ans);
  } else if (m == 2) {
    long long ans = 0;
    for (i = 0; i < n - 1; 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++;
    }
    return !printf("%lld", ans);
  }
  for (i = 1; i < m - 1; i++) c.push_back(b[i]);
  pi[0] = -1;
  int now = -1;
  for (i = 1; i < c.size(); i++) {
    while (now + 1 && c[i] != c[now + 1]) {
      now = pi[now];
    }
    if (c[i] == c[now + 1]) now++;
    pi[i] = now;
  }
  now = -1;
  for (i = 0; i < n; i++) {
    while (now == c.size() - 1 || (now + 1 && a[i] != c[now + 1])) {
      now = pi[now];
    }
    if (a[i] == c[now + 1]) now++;
    if (now == c.size() - 1) mat[i] = 1;
  }
  long long ans = 0;
  for (i = m - 2; i < n - 1; i++)
    if (mat[i] == 1) {
      if (a[i - (m - 2)].second == b[0].second &&
          a[i + 1].second == b[m - 1].second &&
          a[i - (m - 2)].first >= b[0].first &&
          a[i + 1].first >= b[m - 1].first)
        ans++;
    }
  cout << ans;
  return 0;
}
