#include <bits/stdc++.h>
using namespace std;
vector<long long> kmp(vector<pair<long long, char>> s) {
  long long n = s.size();
  vector<long long> p(n, 0);
  for (long long i = 1; i < n; i++) {
    long long j = p[i - 1];
    while (j > 0 && s[j] != s[i]) j = p[j - 1];
    if (s[j] == s[i]) j++;
    p[i] = j;
  }
  return p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, char>> arr, a, b, tot;
  long long x;
  char c, t;
  for (long long i = 0; i < n; i++) {
    cin >> x >> t >> c;
    arr.push_back({x, c});
  }
  a.push_back(arr[0]);
  for (long long i = 1; i < n; i++) {
    if (arr[i].second == arr[i - 1].second)
      a.back().first += arr[i].first;
    else
      a.push_back(arr[i]);
  }
  arr.clear();
  for (long long i = 0; i < m; i++) {
    cin >> x >> t >> c;
    arr.push_back({x, c});
  }
  b.push_back(arr[0]);
  for (long long i = 1; i < m; i++) {
    if (arr[i].second == arr[i - 1].second)
      b.back().first += arr[i].first;
    else
      b.push_back(arr[i]);
  }
  n = a.size(), m = b.size();
  long long ans = 0;
  if (m > 2) {
    for (long long i = 1; i < m - 1; i++) tot.push_back(b[i]);
    tot.push_back({0, '#'});
    for (long long i = 0; i < n; i++) tot.push_back(a[i]);
    long long ans = 0;
    vector<long long> p = kmp(tot);
    for (long long i = m - 1; i < p.size(); i++)
      if (p[i] == m - 2)
        if ((i + 1 < p.size()) && (i - p[i] >= m - 1) &&
            (tot[i - p[i]].second == b[0].second) &&
            (tot[i + 1].second == b[m - 1].second))
          if ((tot[i - p[i]].first >= b[0].first) &&
              (tot[i + 1].first >= b[m - 1].first))
            ans++;
    cout << ans << "\n";
  } else if (m == 2) {
    for (long long i = 1; i < n; i++) {
      if (a[i - 1].second == b[0].second && a[i].second == b[1].second &&
          a[i].first >= b[1].first && a[i - 1].first >= b[0].first)
        ans++;
    }
    cout << ans << "\n";
  } else {
    for (long long i = 0; i < n; i++)
      if (a[i].second == b[0].second)
        ans += max(0ll, a[i].first - b[0].first + 1);
    cout << ans << "\n";
  }
}
