#include <bits/stdc++.h>
using namespace std;
vector<pair<char, long long int> > v1, v2;
int len1, len2, f = 0;
long long int ans;
int lps[200010];
long long int l[200010];
char c[200010];
void computeLPSArray() {
  int len = 0;
  int i;
  lps[0] = 0;
  i = 1;
  while (i < len2) {
    if (v2[i].first == v2[len].first) {
      len++;
      lps[i] = len;
      i++;
    } else {
      if (len != 0) {
        len = lps[len - 1];
      } else {
        lps[i] = 0;
        i++;
      }
    }
  }
}
void KMPSearch() {
  int j = 0;
  int i = 0;
  while (i < len1) {
    if (!j && v2[j].first == v1[i].first && v2[j].second <= v1[i].second) {
      if (f) ans += (long long int)v1[i].second - v2[j].second;
      j++;
      i++;
    } else if (j == len2 - 1 && v2[j].first == v1[i].first &&
               v2[j].second <= v1[i].second) {
      i++;
      j++;
    } else if (j && v2[j] == v1[i]) {
      j++;
      i++;
    }
    if (j == len2) {
      j = lps[j - 1];
      ans++;
    } else if (i < len1 && v2[j] != v1[i]) {
      if (j == len2 - 1 && v2[j].first == v1[i].first &&
          v2[j].second <= v1[i].second)
        continue;
      if (j != 0)
        j = lps[j - 1];
      else
        i = i + 1;
    }
  }
}
int main() {
  int n, m, j;
  long long int sum;
  cin >> n >> m;
  for (int i = 0; i < n; i++) scanf("%l64d-%c", &l[i], &c[i]);
  for (int i = 0; i < n; i++) {
    j = i;
    sum = 0;
    while (j < n && c[j] == c[i]) {
      sum += l[j];
      j++;
    }
    v1.push_back(make_pair(c[i], sum));
    len1++;
    i = j - 1;
  }
  for (int i = 0; i < m; i++) scanf("%d-%c", &l[i], &c[i]);
  for (int i = 0; i < m; i++) {
    j = i;
    sum = 0;
    while (j < m && c[j] == c[i]) {
      sum += l[j];
      j++;
    }
    v2.push_back(make_pair(c[i], sum));
    len2++;
    i = j - 1;
  }
  if (len2 == 1) f = 1;
  computeLPSArray();
  KMPSearch();
  cout << ans << "\n";
  return 0;
}
