#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<long long, int> > d, d2;
int pi[200002];
bool mtch[200002];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  d2.push_back({-1, -1});
  d.push_back({-1, -1});
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    int lit = s.back() - 'a';
    int poz = 0;
    int nr = 0;
    while (s[poz] >= '0' && s[poz] <= '9') nr = nr * 10 + (s[poz] - '0'), ++poz;
    if (d.empty() || d.back().second != lit)
      d.push_back({nr, lit});
    else
      d.back().first += nr;
  }
  for (int i = 1; i <= m; ++i) {
    string s;
    cin >> s;
    int lit = s.back() - 'a';
    int poz = 0;
    int nr = 0;
    while (s[poz] >= '0' && s[poz] <= '9') nr = nr * 10 + (s[poz] - '0'), ++poz;
    if (d2.empty() || d2.back().second != lit)
      d2.push_back({nr, lit});
    else
      d2.back().first += nr;
  }
  long long ans = 0;
  if (d2.size() == 2) {
    for (int i = 0; i < d.size(); ++i)
      if (d[i].second == d2[1].second)
        ans += max(0LL, d[i].first - d2[1].first + 1);
    cout << ans << '\n';
  } else {
    vector<pair<long long, int> > a, b;
    a.push_back({-1, -1});
    b.push_back({-1, -1});
    for (int i = 2; i + 1 < d2.size(); ++i) a.push_back(d2[i]);
    for (int i = 1; i < d.size(); ++i) b.push_back(d[i]);
    int k = 0;
    for (int i = 2; i < a.size(); ++i) {
      while (k && a[k + 1] != a[i]) k = pi[k];
      if (a[k + 1] == a[i]) k++;
      pi[i] = k;
    }
    int q = 0;
    for (int i = 1; i < b.size(); ++i) {
      while (q && a[q + 1] != b[i]) q = pi[q];
      if (a[q + 1] == b[i]) ++q;
      if (q == a.size() - 1) mtch[i - a.size() + 2] = 1;
    }
    for (int stpoz = 1; stpoz + d2.size() - 2 < d.size(); ++stpoz) {
      if (d[stpoz].second == d2[1].second && d[stpoz].first >= d2[1].first) {
        if (d[stpoz + d2.size() - 2].second == d2[d2.size() - 1].second &&
            d[stpoz + d2.size() - 2].first >= d2[d2.size() - 1].first) {
          if (d2.size() == 3)
            ++ans;
          else if (mtch[stpoz + 1])
            ++ans;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
