#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<long long, char> s[400000], t[200000];
int sf[400000];
void read_message(int x, pair<long long, char> *a, int *len) {
  char prevc = 0;
  for (int i = 0; i < x; i++) {
    int j;
    long long b = 0;
    char c;
    char q[10];
    scanf("%s ", q);
    for (j = 0; q[j] != '-'; j++) b = b * 10 + (q[j] - '0');
    c = q[j + 1];
    if (c == prevc) {
      a[(*len) - 1].first += b;
    } else {
      a[(*len)++] = {b, c};
    }
    prevc = c;
  }
}
int main() {
  int _n, _m;
  scanf("%d %d", &_n, &_m);
  read_message(_n, s, &n);
  read_message(_m, t, &m);
  long long ans = 0;
  if (m == 1) {
    for (int i = 0; i < n; i++) {
      if (s[i].second == t[0].second && s[i].first >= t[0].first) {
        ans += s[i].first + 1 - t[0].first;
      }
    }
  } else if (m == 2) {
    for (int i = 0; i < n - 1; i++) {
      if (s[i].second == t[0].second && s[i + 1].second == t[1].second &&
          s[i].first >= t[0].first && s[i + 1].first >= t[1].first) {
        ans++;
      }
    }
  } else {
    int w = n;
    s[w++] = {-1, '$'};
    for (int i = 1; i < m - 1; i++) {
      s[w++] = t[i];
    }
    for (int i = w - 2; i >= 0; i--) {
      int q = sf[i + 1];
      while (q > 0 && s[i] != s[w - q - 1]) q = sf[w - q];
      if (q > 0)
        sf[i] = q + 1;
      else
        sf[i] = (s[i] == s[w - 1]);
    }
    for (int i = 1; i < n - (m - 2); i++) {
      if (sf[i] == m - 2) {
        if (s[i - 1].second == t[0].second &&
            s[i + m - 2].second == t[m - 1].second &&
            s[i - 1].first >= t[0].first &&
            s[i + m - 2].first >= t[m - 1].first) {
          ans++;
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
