#include <bits/stdc++.h>
using namespace std;
const long long N = 4e5 + 5, inf = 1e9 + 7;
long long n, k;
long long in(vector<pair<long long, long long> >& v, long long n) {
  string s;
  cin >> s;
  v.emplace_back(pair<long long, long long>(0, s.back() - 'a'));
  long long p = 0;
  s = s.substr(0, s.size() - 2);
  v[p].first = stoi(s);
  for (long long i = 1; i < n; i++) {
    cin >> s;
    if (s.back() - 'a' != v[p].second)
      v.emplace_back(pair<long long, long long>(0, s.back() - 'a')), p++;
    s = s.substr(0, s.size() - 2);
    v[p].first += stoi(s);
  }
  return p + 1;
}
long long prefix_function(vector<pair<long long, long long> > v) {
  long long n = (long long)v.size();
  long long ans = 0;
  vector<long long> pi(n);
  for (long long i = 1; i < n; i++) {
    long long j = pi[i - 1];
    if (j == k - 1 && v[i].second == v[j].second && v[i].first >= v[j].first)
      ans++;
    while (j > 0 && v[i] != v[j]) j = pi[j - 1];
    if (v[i] == v[j])
      j++;
    else if (j == 0 && v[i].second == v[j].second && v[i].first > v[j].first)
      j++;
    pi[i] = j;
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long i, j, a, b, ans = 0;
  cin >> n >> k;
  vector<pair<long long, long long> > v, u;
  n = in(v, n);
  k = in(u, k);
  if (k == 1) {
    for (i = 0; i < n; i++)
      if (v[i].second == u[0].second)
        ans += max(0LL, v[i].first - u[0].first + 1);
    return cout << ans, 0;
  }
  u.emplace_back(pair<long long, long long>(1, 29));
  copy(v.begin(), v.end(), back_inserter(u));
  cout << prefix_function(u);
}
