#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1) ret = (ret * a) % MMM;
  return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int ddx[] = {0, 0, 1, 1, 1, -1, -1, -1}, ddy[] = {1, -1, 1, 0, -1, 1, 0, -1};
int n, m;
vector<pair<long long, char> > a, b;
int pi[200005];
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = (1); (i) <= (n); (i) += (1)) {
    long long x;
    char c;
    scanf("%lld-%c ", &x, &c);
    if (a.empty())
      a.push_back(make_pair(x, c));
    else if (a.back().second == c)
      a.back().first += x;
    else
      a.push_back(make_pair(x, c));
  }
  for (int(i) = (1); (i) <= (m); (i) += (1)) {
    long long x;
    char c;
    scanf("%lld-%c ", &x, &c);
    if (b.empty())
      b.push_back(make_pair(x, c));
    else if (b.back().second == c)
      b.back().first += x;
    else
      b.push_back(make_pair(x, c));
  }
  n = a.size(), m = b.size();
  if (n < m) return !printf("0");
  long long ans = 0;
  if (m == 1) {
    for (int(i) = (0); (i) <= (n - 1); (i) += (1))
      if (a[i].second == b[0].second)
        ans += max(0LL, a[i].first - b[0].first + 1);
    return !printf("%lld", ans);
  }
  if (m == 2) {
    for (int(i) = (0); (i) <= (n - 2); (i) += (1))
      if (b[0].second == a[i].second && b[1].second == a[i + 1].second &&
          b[0].first <= a[i].first && b[1].first <= a[i + 1].first)
        ans++;
    return !printf("%lld", ans);
  }
  pi[1] = 1;
  for (int i = 2, j = 1; i < m - 1; i++) {
    while (j > 1 && b[i] != b[j]) j = pi[j - 1];
    if (b[i] == b[j]) pi[i] = ++j;
  }
  for (int i = 1, j = 1; i < n - 1; i++) {
    while (j > 1 && a[i] != b[j]) j = pi[j - 1];
    if (a[i] == b[j]) {
      if (j == m - 2) {
        if (a[i + 1].second == b[m - 1].second &&
            b[0].second == a[i - m + 2].second &&
            a[i + 1].first >= b[m - 1].first &&
            b[0].first <= a[i - m + 2].first)
          ans++;
        j = pi[j];
      } else
        j++;
    }
  }
  printf("%lld", ans);
}
