#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
char str[200007], ch[200007], tmp, tstr[200007];
unsigned long long int ha[200007], hb, x[200007], y, H[200007], a[200007],
    b[200007], ta[200007];
void pre_has() {
  H[0] = 1;
  for (int i = 1; i < 200007; i++) H[i] = H[i - 1] * 10000007;
}
int main() {
  ios::sync_with_stdio(false);
  pre_has();
  unsigned long long int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> ta[i] >> tmp >> tstr[i];
  }
  int in = 0;
  str[0] = tstr[0];
  a[0] = ta[0];
  for (int i = 1; i < n; i++) {
    if (str[in] == tstr[i]) {
      a[in] += ta[i];
    } else {
      a[++in] = ta[i];
      str[in] = tstr[i];
    }
  }
  n = in + 1;
  ha[0] = str[0];
  for (int i = 1; i < n; i++) ha[i] = (ha[i - 1] * 10000007) + str[i];
  x[0] = a[0];
  for (int i = 1; i < n; i++) x[i] = (x[i - 1] * 10000007) + a[i];
  for (int i = 0; i < m; i++) {
    cin >> ta[i] >> tmp >> tstr[i];
  }
  in = 0;
  ch[0] = tstr[0];
  b[0] = ta[0];
  for (int i = 1; i < m; i++) {
    if (ch[in] == tstr[i]) {
      b[in] += ta[i];
    } else {
      b[++in] = ta[i];
      ch[in] = tstr[i];
    }
  }
  m = in + 1;
  hb = ch[0];
  for (int i = 1; i < m; i++) hb = (hb * 10000007) + ch[i];
  for (int i = 1; i < m - 1; i++) y = (y * 10000007) + b[i];
  if (n < m) {
    cout << 0 << endl;
    return 0;
  }
  unsigned long long int ans = 0, ck, e, f;
  if (m == 1) {
    for (int i = 0; i < n; i++) {
      if (ch[0] == str[i] && a[i] >= b[0]) {
        ans += a[i] - b[0] + 1;
      }
    }
  } else {
    for (int i = 0; i <= n - m; i++) {
      ck = i > 0 ? ha[i + m - 1] - (ha[i - 1] * H[m]) : ha[i + m - 1];
      if (ck == hb && a[i] >= b[0] && a[i + m - 1] >= b[m - 1]) {
        int ix = i + 1, iy = i + m - 2;
        if (ix <= iy) {
          e = ix > 0 ? x[iy] - (x[ix - 1] * H[iy - ix + 1]) : x[iy];
          if (e == y) ans++;
        } else
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
