#include <bits/stdc++.h>
using namespace std;
struct dat {
  long long l;
  char c;
  friend bool operator<=(const dat &d1, const dat &d2) {
    return (d1.c == d2.c) && (d1.l <= d2.l);
  }
  friend bool operator==(const dat &d1, const dat &d2) {
    return (d1.c == d2.c) && (d1.l == d2.l);
  }
} a[200001], b[200001];
int f[200001], n, m;
void cp(dat *a, int &n) {
  int m = 0;
  for (int i = 0; i < n; ++i) {
    if (m == 0 || a[m - 1].c != a[i].c)
      a[m++] = a[i];
    else
      a[m - 1].l += a[i].l;
  }
  n = m;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%lld-%c", &a[i].l, &a[i].c);
  for (int i = 0; i < m; ++i) scanf("%lld-%c", &b[i].l, &b[i].c);
  cp(a, n);
  cp(b, m);
  long long ans = 0;
  switch (m) {
    case 1: {
      for (int i = 0; i < n; ++i)
        if (b[0] <= a[i]) ans += a[i].l - b[0].l + 1;
      break;
    }
    case 2: {
      for (int i = 0; i < n - 1; ++i)
        if (b[0] <= a[i] && b[1] <= a[i + 1]) ++ans;
      break;
    }
    default: {
      f[1] = 0;
      for (int i = 2; i < m - 1; i++) {
        int j = f[i - 1];
        while (j > 0 && !(b[j + 1] == b[i])) j = f[j];
        if (b[j + 1] == b[i]) j++;
        f[i] = j;
      }
      for (int i = 1, j = 0; i < n - 1; i++) {
        while (j > 0 && !(b[j + 1] == a[i])) j = f[j];
        if (b[j + 1] == a[i]) j++;
        if (j == m - 2) {
          if (b[0] <= a[i - j] && b[j + 1] <= a[i + 1]) ans++;
          j = f[j];
        }
      }
    }
  }
  printf("%lld\n", ans);
}
