#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, char> > s1;
  vector<pair<long long, char> > s2;
  for (long long i = 0; i < n; i++) {
    int len;
    char let;
    scanf("%d-%c", &len, &let);
    if (s1.size() > 0 && s1.back().second == let)
      s1.back().first += len;
    else
      s1.push_back(pair<long long, char>(len, let));
  }
  for (long long i = 0; i < m; i++) {
    int len;
    char let;
    scanf("%d-%c", &len, &let);
    if (s2.size() > 0 && s2.back().second == let)
      s2.back().first += len;
    else
      s2.push_back(pair<long long, char>(len, let));
  }
  long long ans = 0;
  if (s2.size() == 1) {
    for (long long i = 0; i < s1.size(); i++) {
      if (s1[i].second == s2[0].second) {
        ans += max(0LL, s1[i].first - s2[0].first + 1);
      }
    }
    cout << ans << endl;
    return 0;
  }
  long long has[s1.size() + 1];
  has[0] = 0;
  long long desired_has = 0;
  long long p = 1;
  for (long long i = 0; i < s1.size(); i++) {
    has[i + 1] = p * ((s1[i].second - 97LL) * 1000001LL + s1[i].first) + has[i];
    p *= 26000007LL;
  }
  p = 1;
  for (long long i = 1; i < s2.size() - 1; i++) {
    desired_has += ((s2[i].second - 97) * 1000001LL + s2[i].first) * p;
    p *= 26000007LL;
  }
  for (long long i = 0; i + s2.size() <= s1.size(); i++) {
    desired_has *= 26000007LL;
    if (s1[i].second == s2[0].second &&
        s1[i + s2.size() - 1].second == s2.back().second &&
        s1[i].first >= s2[0].first &&
        s1[i + s2.size() - 1].first >= s2.back().first &&
        desired_has == has[i + s2.size() - 1] - has[i + 1]) {
      ans++;
    }
  }
  cout << ans << endl;
}
