#include <bits/stdc++.h>
using namespace std;
const long long MN = 200111;
long long ns, nt, nr;
pair<long long, char> s[MN], t[MN], r[MN];
long long next___[MN];
void compress(pair<long long, char> s[], long long& ns) {
  long long n = 0;
  for (long long i = (1), _b = (ns); i <= _b; ++i) {
    if (i == 1 || s[i].second != s[n].second) {
      s[++n] = s[i];
    } else {
      s[n].first += s[i].first;
    }
  }
  ns = n;
}
long long solve1() {
  assert(nt == 1);
  long long res = 0;
  for (long long i = (1), _b = (ns); i <= _b; ++i) {
    if (s[i].second == t[1].second && s[i].first >= t[1].first) {
      res += s[i].first - t[1].first + 1;
    }
  }
  return res;
}
long long solve2() {
  assert(nt == 2);
  long long res = 0;
  for (long long i = (1), _b = (ns - 1); i <= _b; ++i) {
    if (s[i].second == t[1].second && s[i].first >= t[1].first &&
        s[i + 1].second == t[2].second && s[i + 1].first >= t[2].first) {
      ++res;
    }
  }
  return res;
}
long long solve() {
  if (nt == 1)
    return solve1();
  else if (nt == 2)
    return solve2();
  long long res = 0;
  nr = nt - 2;
  for (long long i = (1), _b = (nr); i <= _b; ++i) r[i] = t[i + 1];
  long long j = 0;
  next___[1] = 0;
  for (long long i = (2), _b = (nr); i <= _b; ++i) {
    while (j > 0 && r[i] != r[j + 1]) j = next___[j];
    if (r[i] == r[j + 1]) ++j;
    next___[i] = j;
  }
  j = 0;
  for (long long i = (1), _b = (ns); i <= _b; ++i) {
    while (j > 0 && s[i] != r[j + 1]) j = next___[j];
    if (s[i] == r[j + 1]) ++j;
    if (j == nr) {
      long long from = (i - nr + 1) - 1;
      long long to = i + 1;
      if (from >= 1 && s[from].first >= t[1].first &&
          s[from].second == t[1].second && to <= ns &&
          s[to].first >= t[nt].first && s[to].second == t[nt].second)
        ++res;
      j = next___[j];
    }
  }
  return res;
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  while (cin >> ns >> nt) {
    for (long long i = (1), _b = (ns); i <= _b; ++i) {
      char tmp;
      cin >> s[i].first >> tmp >> s[i].second;
    }
    for (long long i = (1), _b = (nt); i <= _b; ++i) {
      char tmp;
      cin >> t[i].first >> tmp >> t[i].second;
    }
    compress(s, ns);
    compress(t, nt);
    cout << solve() << endl;
  }
}
