#include <bits/stdc++.h>
using namespace std;
long long cnt[400111];
char c[400111];
long long cntn[200111];
char cn[200111];
long long cntm[200111];
char cm[200111];
int pf[400111];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int i = 0;
  while (n-- > 0) {
    int ccnt;
    char cc;
    scanf("%d-%c", &ccnt, &cc);
    if (i == 0) {
      cntn[i] = ccnt;
      cn[i] = cc;
      i++;
      continue;
    }
    if (cc == cn[i - 1]) {
      cntn[i - 1] += ccnt;
    } else {
      cntn[i] = ccnt;
      cn[i] = cc;
      i++;
    }
  }
  n = i;
  i = 0;
  while (m-- > 0) {
    int ccnt;
    char cc;
    scanf("%d-%c", &ccnt, &cc);
    if (i == 0) {
      cntm[i] = ccnt;
      cm[i] = cc;
      i++;
      continue;
    }
    if (cc == cm[i - 1]) {
      cntm[i - 1] += ccnt;
    } else {
      cntm[i] = ccnt;
      cm[i] = cc;
      i++;
    }
  }
  m = i;
  long long ans = 0;
  if (m == 1) {
    for (int i = 0; i < n; i++) {
      if (cn[i] == cm[0]) {
        ans += max(0ll, cntn[i] - cntm[0] + 1);
      }
    }
  } else if (m == 2) {
    for (int i = 0; i < n - 1; i++) {
      if (cn[i] == cm[0] && cn[i + 1] == cm[1]) {
        ans += (cntn[i] >= cntm[0]) && (cntn[i + 1] >= cntm[1]);
      }
    }
  } else {
    int ii = 0;
    for (int i = 1; i < m - 1; i++) {
      c[ii] = cm[i];
      cnt[ii] = cntm[i];
      ii++;
    }
    ii++;
    for (int i = 0; i < n; i++) {
      c[ii] = cn[i];
      cnt[ii] = cntn[i];
      ii++;
    }
    for (int i = 1; i < ii; i++) {
      int cf = pf[i - 1];
      while (cf > 0 && (c[cf] != c[i] || cnt[cf] != cnt[i])) {
        cf = pf[cf - 1];
      }
      if (!(c[cf] != c[i] || cnt[cf] != cnt[i])) {
        cf++;
      }
      pf[i] = cf;
      if (pf[i] == m - 2) {
        if (i - m + 2 >= 0) {
          if (c[i + 1] == cm[m - 1] && cnt[i + 1] >= cntm[m - 1] &&
              c[i - m + 2] == cm[0] && cnt[i - m + 2] >= cntm[0]) {
            ++ans;
          }
        }
      }
    }
  }
  cout << ans << endl;
}
