#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, char> > s, t, tmp;
vector<long long> p;
long long n, m, k, x, ans, ctr;
char lst, a;
int main() {
  cin >> n >> m;
  for (long i = 0; i < n; i++) {
    cin >> x >> a >> a;
    if (!i) {
      lst = a;
      k = x;
      continue;
    }
    if (lst != a) {
      s.push_back(make_pair(k, lst));
      k = x;
      lst = a;
    } else
      k += x;
  }
  s.push_back(make_pair(k, lst));
  for (long i = 0; i < m; i++) {
    cin >> x >> a >> a;
    if (!i) {
      lst = a;
      k = x;
      continue;
    }
    if (lst != a) {
      t.push_back(make_pair(k, lst));
      k = x;
      lst = a;
    } else
      k += x;
  }
  t.push_back(make_pair(k, lst));
  if (t.size() > s.size()) {
    cout << 0;
    return 0;
  }
  if (t.size() == 1) {
    for (long i = 0; i < s.size(); i++)
      if (t[0].second == s[i].second)
        ans += max(0ll, s[i].first - t[0].first + 1);
    cout << ans;
    return 0;
  }
  if (t.size() == 2) {
    for (long i = 0; i < s.size() - 1; i++)
      if (t[0].second == s[i].second && t[1].second == s[i + 1].second &&
          t[0].first <= s[i].first && t[1].first <= s[i + 1].first)
        ans++;
    cout << ans;
    return 0;
  }
  for (long i = 1; i < t.size() - 1; i++) tmp.push_back(t[i]);
  ctr = tmp.size();
  tmp.push_back(make_pair(0, '#'));
  for (long i = 0; i < s.size(); i++) tmp.push_back(s[i]);
  p.resize(tmp.size());
  p[0] = 0;
  for (long i = 1; i < p.size(); i++) {
    long long j = p[i - 1];
    while (j > 0 && tmp[j] != tmp[i]) j = p[j - 1];
    if (tmp[j] == tmp[i]) j++;
    p[i] = j;
  }
  for (long i = ctr + ctr + 1; i < p.size() - 1; i++) {
    if (ctr == p[i] && t[0].second == s[i - ctr - ctr - 1].second &&
        t[ctr + 1].second == s[i - ctr].second &&
        t[0].first <= s[i - ctr - ctr - 1].first &&
        t[ctr + 1].first <= s[i - ctr].first)
      ans++;
  }
  cout << ans;
  return 0;
}
