#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
long long ca[N], la[N], cb[N], lb[N];
int f[N], g[N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d-%c", la + i, ca + i);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d-%c", lb + i, cb + i);
  }
  int nn = 0;
  for (int i = 1; i < n; i++) {
    if (ca[i] == ca[nn]) {
      la[nn] += la[i];
    } else {
      nn++;
      ca[nn] = ca[i];
      la[nn] = la[i];
    }
  }
  nn++;
  int mm = 0;
  for (int i = 1; i < m; i++) {
    if (cb[i] == cb[mm]) {
      lb[mm] += lb[i];
    } else {
      mm++;
      cb[mm] = cb[i];
      lb[mm] = lb[i];
    }
  }
  mm++;
  if (mm == 1) {
    long long ans = 0;
    for (int i = 0; i < nn; i++) {
      if (ca[i] == cb[0]) {
        if (la[i] >= lb[0]) ans += la[i] - lb[0] + 1;
      }
    }
    printf("%I64d", ans);
    return 0;
  } else if (mm == 2) {
    long long ans = 0;
    for (int i = 1; i < nn; i++) {
      if (ca[i - 1] == cb[0] && ca[i] == cb[1]) {
        if (la[i - 1] >= lb[0] && la[i] >= lb[1]) ans++;
      }
    }
    printf("%I64d", ans);
    return 0;
  }
  f[1] = 0;
  m = mm - 2;
  for (int i = 2; i <= m; i++) {
    int j = f[i - 1];
    while (1) {
      if (lb[i] == lb[j + 1] && cb[i] == cb[j + 1]) {
        f[i] = j + 1;
        break;
      }
      if (j == 0) {
        f[i] = 0;
        break;
      }
      j = f[j];
    }
  }
  long long ans = 0;
  g[0] = 0;
  n = nn - 1;
  for (int i = 1; i < n; i++) {
    int j = g[i - 1];
    if (j == m) j = f[j];
    while (1) {
      if (la[i] == lb[j + 1] && ca[i] == cb[j + 1]) {
        g[i] = j + 1;
        break;
      }
      if (j == 0) {
        g[i] = 0;
        break;
      }
      j = f[j];
    }
    if (g[i] == m) {
      int ii = i - m;
      if (ca[ii] == cb[0] && la[ii] >= lb[0] && ca[i + 1] == cb[m + 1] &&
          la[i + 1] >= lb[m + 1])
        ans++;
    }
  }
  printf("%I64d", ans);
  return 0;
}
