#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 7e5 + 30;
long long n, m, f[MAXN];
vector<pair<long long, char>> vec, sec;
bool mtc[MAXN];
vector<pair<long long, char>> cook(vector<pair<long long, char>> in) {
  vector<pair<long long, char>> ret;
  for (long long i = 0; i < (long long)in.size();) {
    long long j = i, sz = 0;
    while (j < (long long)in.size() && in[i].second == in[j].second)
      sz += in[j++].first;
    ret.push_back({sz, in[i].second});
    i = j;
  }
  return ret;
}
void find_your_partner() {
  long long k = 0, sz = (long long)sec.size() - 2;
  for (long long i = 1; i < sz; i++) {
    while (k && (sec[i + 1].first != sec[k + 1].first ||
                 sec[i + 1].second != sec[k + 1].second))
      k = f[k];
    if (sec[k + 1].first == sec[i + 1].first &&
        sec[i + 1].second == sec[k + 1].second)
      k++;
    f[i + 1] = k;
  }
  k = 0;
  for (long long i = 0; i < (long long)vec.size(); i++) {
    while (k && (vec[i].first != sec[k + 1].first ||
                 vec[i].second != sec[k + 1].second))
      k = f[k];
    if (sec[k + 1].first == vec[i].first && vec[i].second == sec[k + 1].second)
      k++;
    if (k == sz) {
      mtc[i] = 1;
      k = f[k];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  vec.resize(n);
  sec.resize(m);
  char wasted;
  for (long long i = 0; i < n; i++)
    cin >> vec[i].first >> wasted >> vec[i].second;
  for (long long i = 0; i < m; i++)
    cin >> sec[i].first >> wasted >> sec[i].second;
  vec = cook(vec);
  sec = cook(sec);
  m = sec.size();
  n = vec.size();
  if ((long long)sec.size() > 2) find_your_partner();
  long long ans = 0;
  for (long long i = 0; i <= (long long)vec.size() - (long long)sec.size();
       i++) {
    if (sec.size() == 1) {
      if (sec[0].second == vec[i].second && vec[i].first >= sec[0].first)
        ans += vec[i].first - sec[0].first + 1;
    } else if (sec.size() == 2) {
      if (i < (long long)vec.size() - 1 && vec[i].second == sec[0].second &&
          vec[i + 1].second == sec[1].second && vec[i].first >= sec[0].first &&
          vec[i + 1].first >= sec[1].first)
        ans++;
    } else if (sec[0].second == vec[i].second && vec[i].first >= sec[0].first)
      if (mtc[i + (long long)sec.size() - 2] &&
          vec[i + (long long)sec.size() - 1].second == sec[m - 1].second &&
          vec[i + (long long)sec.size() - 1].first >= sec[m - 1].first)
        ans++;
  }
  cout << ans << "\n";
  return 0;
}
