#include <bits/stdc++.h>
using namespace std;
int n, m, p[200010];
long long ans;
struct A {
  long long t;
  char c;
  void in() {
    scanf(
        "%I64d"
        "-%c",
        &t, &c);
  }
} a[200010], b[200010];
bool same(A i, A j) { return i.t == j.t && i.c == j.c; }
bool big(A i, A j) { return i.c == j.c && i.t >= j.t; }
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) a[i].in();
  for (int i = 1; i <= m; i++) b[i].in();
  int t = 0;
  for (int i = 1; i <= n; i++)
    if (!t)
      a[++t] = a[i];
    else if (a[t].c == a[i].c)
      a[t].t += a[i].t;
    else
      a[++t] = a[i];
  n = t;
  t = 0;
  for (int i = 1; i <= m; i++)
    if (!t)
      b[++t] = b[i];
    else if (b[t].c == b[i].c)
      b[t].t += b[i].t;
    else
      b[++t] = b[i];
  m = t;
  if (m == 1) {
    for (int i = 1; i <= n; i++)
      if (big(a[i], b[1])) ans += a[i].t - b[1].t + 1;
  } else if (m == 2) {
    for (int i = 1; i + m - 1 <= n; i++) {
      bool ok = 1;
      for (int j = 1; j <= m; j++) ok &= big(a[i + j - 1], b[j]);
      ans += ok;
    }
  } else {
    p[2] = 1;
    for (int i = 3, j = 1; i < m; i++) {
      while (j > 1 && !same(b[j + 1], b[i])) j = p[j];
      if (same(b[j + 1], b[i])) j++;
      p[i] = j;
    }
    for (int i = 2, j = 1; i < n; i++) {
      while (j == m - 1 || (j > 1 && !same(b[j + 1], a[i]))) j = p[j];
      if (same(b[j + 1], a[i])) j++;
      if (j == m - 1 && big(a[i - (m - 1) + 1], b[1]) && big(a[i + 1], b[m]))
        ans++;
    }
  }
  printf("%I64d", ans);
  return 0;
}
