#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m, i, j, t, p;
  cin >> n >> m;
  t = p = 0;
  vector<pair<long long, char>> text, pattern;
  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    long long l = stoi(s.substr(0, s.size() - 2));
    char ch = s.back();
    if (!t || text[t - 1].second != ch) {
      text.push_back({l, ch});
      ++t;
    } else
      text[t - 1].first += l;
  }
  for (i = 0; i < m; i++) {
    string s;
    cin >> s;
    long long l = stoi(s.substr(0, s.size() - 2));
    char ch = s.back();
    if (!p || pattern[p - 1].second != ch) {
      pattern.push_back({l, ch});
      ++p;
    } else
      pattern[p - 1].first += l;
  }
  long long ans = 0;
  if (p == 1) {
    long long l = pattern[0].first;
    char ch = pattern[0].second;
    for (i = 0; i < t; i++)
      if (text[i].second == ch && text[i].first >= l)
        ans += (text[i].first - l + 1);
  } else if (p == 2) {
    for (i = 1; i < t; i++)
      if (text[i - 1].second == pattern[0].second &&
          text[i - 1].first >= pattern[0].first &&
          text[i].second == pattern[1].second &&
          text[i].first >= pattern[1].first)
        ++ans;
  } else {
    vector<pair<long long, char>> s;
    for (i = 1; i < p - 1; i++)
      s.push_back({pattern[i].first, pattern[i].second});
    s.push_back({0, '$'});
    for (i = 0; i < t; i++) s.push_back({text[i].first, text[i].second});
    n = s.size();
    vector<long long> zarr(n, 0);
    long long l, r;
    for (i = 1, l = r = 0; i < n; i++) {
      if (i > r) {
        l = r = i;
        while (r < n && s[r] == s[r - l]) ++r;
        --r;
        zarr[i] = r - l + 1;
      } else {
        if (zarr[i - l] < r - i + 1)
          zarr[i] = zarr[i - l];
        else {
          l = i;
          while (r < n && s[r] == s[r - l]) ++r;
          --r;
          zarr[i] = r - l + 1;
        }
      }
    }
    for (i = p; i + p - 2 < n; ++i)
      if (s[i - 1].second == pattern[0].second &&
          s[i - 1].first >= pattern[0].first &&
          s[i + p - 2].second == pattern[p - 1].second &&
          s[i + p - 2].first >= pattern[p - 1].first && zarr[i] == p - 2)
        ++ans;
  }
  cout << ans;
}
