#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
pair<long long, char> s[N], t[N];
int sl, tl, n, m, nxt[N];
void calnxt() {
  nxt[1] = 0;
  for (int i = 2; i < tl - 1; i++) {
    int j = nxt[i - 1];
    while (j > 0 && t[j + 1] != t[i]) j = nxt[j];
    if (t[j + 1] == t[i])
      nxt[i] = j + 1;
    else
      nxt[i] = 0;
  }
}
long long solve() {
  long long ret = 0;
  int ps = 0, pt = 1;
  while (ps < sl) {
    if (s[ps] == t[pt]) {
      ps++;
      pt++;
    } else {
      if (pt == 1)
        ps++;
      else {
        pt = nxt[pt - 1] + 1;
      }
    }
    if (pt == tl - 1) {
      if (ps < sl && s[ps].first >= t[pt].first &&
          s[ps].second == t[pt].second && ps - tl + 1 >= 0 &&
          s[ps - tl + 1].first >= t[0].first &&
          s[ps - tl + 1].second == t[0].second)
        ret++;
      pt = nxt[pt - 1] + 1;
    }
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int f;
    char p[5];
    scanf("%d%s", &f, p);
    if (sl == 0 || p[1] != s[sl - 1].second) {
      s[sl].first = f;
      s[sl++].second = p[1];
    } else {
      s[sl - 1].first += f;
    }
  }
  for (int i = 0; i < m; i++) {
    int f;
    char p[5];
    scanf("%d%s", &f, p);
    if (tl == 0 || p[1] != t[tl - 1].second) {
      t[tl].first = f;
      t[tl++].second = p[1];
    } else {
      t[tl - 1].first += f;
    }
  }
  long long ans = 0;
  if (tl == 1) {
    for (int i = 0; i < sl; i++) {
      if (s[i].second == t[0].second && s[i].first >= t[0].first) {
        ans += s[i].first - t[0].first + 1;
      }
    }
  } else if (tl == 2) {
    for (int i = 0; i < sl - 1; i++) {
      if (s[i].second == t[0].second && s[i].first >= t[0].first &&
          s[i + 1].second == t[1].second && s[i + 1].first >= t[1].first)
        ans++;
    }
  } else {
    calnxt();
    ans = solve();
  }
  cout << ans << endl;
  return 0;
}
