#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, char> > s, t, u;
long long lps[200005];
void computelps() {
  long long i, j, k;
  long long n1, n2;
  n2 = u.size();
  lps[0] = 0;
  i = 1;
  j = 0;
  while (i < n2) {
    if (u[i] == u[j]) {
      lps[i] = j + 1;
      i++;
      j++;
    } else {
      if (j == 0) {
        lps[i] = 0;
        i++;
      } else {
        j = lps[j - 1];
      }
    }
  }
}
long long match() {
  long long i, j, k;
  long long n1, n2;
  n1 = s.size();
  n2 = u.size();
  i = 0;
  j = 0;
  long long ans = 0;
  while (i < n1) {
    if (s[i] == u[j]) {
      j++;
      i++;
      if (j == n2) {
        if (i - n2 - 1 >= 0 && s[i - n2 - 1].second == t[0].second &&
            s[i - n2 - 1].first >= t[0].first && i < n1 &&
            s[i].second == t[n2 + 1].second && s[i].first >= t[n2 + 1].first)
          ans++;
        j = lps[j - 1];
      }
    } else {
      if (j == 0)
        i++;
      else
        j = lps[j - 1];
    }
  }
  return ans;
}
int main() {
  long long i, j, k;
  long long n, m;
  char c;
  scanf("%lld", &n);
  scanf("%lld", &m);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &j);
    c = getchar();
    c = getchar();
    if (i == 1)
      s.push_back(make_pair(j, c));
    else if (c == s[s.size() - 1].second)
      s[s.size() - 1].first += j;
    else
      s.push_back(make_pair(j, c));
  }
  for (i = 1; i <= m; i++) {
    scanf("%lld", &j);
    c = getchar();
    c = getchar();
    if (i == 1)
      t.push_back(make_pair(j, c));
    else if (c == t[t.size() - 1].second)
      t[t.size() - 1].first += j;
    else
      t.push_back(make_pair(j, c));
  }
  long long ans = 0;
  if (t.size() == 1) {
    for (i = 0; i < s.size(); i++) {
      if (t[0].second == s[i].second && t[0].first <= s[i].first) {
        ans += s[i].first - t[0].first + 1;
      }
    }
  } else if (t.size() == 2) {
    for (i = 0; i < s.size() - 1; i++) {
      if (t[0].second == s[i].second && t[0].first <= s[i].first &&
          t[1].second == s[i + 1].second && t[1].first <= s[i + 1].first)
        ans++;
    }
  } else {
    for (i = 1; i < t.size() - 1; i++) u.push_back(t[i]);
    computelps();
    ans = match();
  }
  printf("%lld\n", ans);
  return 0;
}
