#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, m, back[N];
vector<string> T, P;
char z[111];
vector<int> with;
vector<pair<long long, char> > a, b;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0, x; i < n; ++i) {
    char y;
    scanf("%d- %c", &x, &y);
    if (a.size() != 0 && a.back().second == y) {
      a.back().first += x;
    } else {
      a.push_back(make_pair(x, y));
    }
  }
  n = a.size();
  for (int i = 0; i < a.size(); ++i) {
    sprintf(z, "%lld%c", a[i].first, a[i].second);
    T.push_back(z);
  }
  for (int i = 0, x; i < m; ++i) {
    char y;
    scanf("%d- %c", &x, &y);
    if (b.size() != 0 && b.back().second == y) {
      b.back().first += x;
    } else {
      b.push_back(make_pair(x, y));
    }
  }
  m = b.size();
  for (int i = 1; i < m - 1; ++i) {
    sprintf(z, "%lld%c", b[i].first, b[i].second);
    P.push_back(z);
  }
  if (m == 1) {
    long long res = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i].first >= b[0].first && a[i].second == b[0].second) {
        long long l = 1, r = a[i].first, can = 0;
        while (l <= r) {
          long long mid = (l + r) >> 1;
          if (mid + b[0].first - 1 <= a[i].first) {
            can = mid;
            l = mid + 1;
          } else {
            r = mid - 1;
          }
        }
        res += can;
      }
    }
    printf("%lld\n", res);
    return 0;
  }
  if (m == 2) {
    int res = 0;
    for (int i = 0; i + 1 < n; ++i) {
      if (a[i].first < b[0].first || a[i].second != b[0].second) continue;
      if (a[i + 1].first < b[1].first || a[i + 1].second != b[1].second)
        continue;
      ++res;
    }
    printf("%d\n", res);
    return 0;
  }
  int i = 0, j = -1;
  back[0] = -1;
  while (i < P.size()) {
    while (j >= 0 && P[i] != P[j]) {
      j = back[j];
    }
    ++i, ++j;
    back[i] = j;
  }
  i = j = 0;
  while (i < T.size()) {
    while (j >= 0 && T[i] != P[j]) {
      j = back[j];
    }
    ++i, ++j;
    if (j == P.size()) {
      with.push_back(i - j);
      j = back[j];
    }
  }
  int res = 0;
  for (int i = 0; i < with.size(); ++i) {
    int from = with[i] - 1, to = with[i] + P.size();
    if (from < 0 || to >= T.size()) continue;
    if (a[from].first < b[0].first || a[from].second != b[0].second) continue;
    if (a[to].first < b[m - 1].first || a[to].second != b[m - 1].second)
      continue;
    ++res;
  }
  printf("%d\n", res);
  return 0;
}
