#include <bits/stdc++.h>
using namespace std;
char s[5];
vector<pair<long long, char> > V, V1;
long long Z[4 * 100005];
vector<pair<long long, char> > reduce(vector<pair<long long, char> > v) {
  vector<pair<long long, char> > ret;
  int i = 0;
  while (i < v.size()) {
    pair<long long, char> tmp = v[i];
    long long sum = 0;
    while (i < v.size() && tmp.second == v[i].second) {
      sum += v[i].first;
      i++;
    }
    ret.push_back(make_pair(sum, tmp.second));
  }
  return ret;
}
bool equals(pair<long long, char> p1, pair<long long, char> p2) {
  return (p1.first == p2.first && p1.second == p2.second);
}
long long brute(vector<pair<long long, char> > v1,
                vector<pair<long long, char> > v2) {
  long long ans = 0;
  if (v2.size() == 1) {
    for (int i = 0; i < v1.size(); i++) {
      if (v1[i].second == v2[0].second) {
        if (v1[i].first >= v2[0].first) {
          ans += v1[i].first - v2[0].first + 1;
        }
      }
    }
    return ans;
  }
  for (int i = 0; i <= v1.size() - v2.size(); i++) {
    int fl = 1;
    for (int j = 0; j < v2.size(); j++) {
      if (v1[i + j].second != v2[j].second || v1[i + j].first < v2[j].first) {
        fl = 0;
        break;
      }
    }
    if (fl) ans++;
  }
  return ans;
}
int main() {
  int n, m;
  long long x;
  scanf("%d %d", &n, &m);
  pair<long long, char> P;
  for (int i = 0; i < n; i++) {
    scanf("%lld %s", &x, s);
    P.first = x;
    P.second = s[1];
    V.push_back(P);
  }
  for (int i = 0; i < m; i++) {
    scanf("%lld %s", &x, s);
    P.first = x;
    P.second = s[1];
    V1.push_back(P);
  }
  vector<pair<long long, char> > v1, v2;
  v1 = reduce(V);
  v2 = reduce(V1);
  if (v2.size() <= 2) {
    long long ans = brute(v1, v2);
    printf("%lld\n", ans);
    return 0;
  }
  vector<pair<long long, char> > w;
  for (int i = 1; i < v2.size() - 1; i++) {
    w.push_back(v2[i]);
  }
  w.push_back(make_pair(-1, '$'));
  for (int i = 0; i < v1.size(); i++) {
    w.push_back(v1[i]);
  }
  int N = w.size();
  int L = 0, R = 0;
  for (int i = 0; i < N; i++) {
    if (i > R) {
      L = R = i;
      while (R < N && equals(w[R], w[R - L])) {
        R++;
      }
      R--;
      Z[i] = R - L + 1;
    } else {
      int k = i - L;
      if (Z[k] < R - i + 1) {
        Z[i] = Z[k];
      } else {
        L = i;
        while (R < N && equals(w[R], w[R - L])) {
          R++;
        }
        R--;
        Z[i] = R - L + 1;
      }
    }
  }
  vector<pair<int, int> > A;
  for (int i = v2.size() - 1; i < w.size(); i++) {
    if (Z[i] == (v2.size() - 2)) {
      int st = i - (v2.size() - 1);
      int en = st + v2.size() - 3;
      A.push_back(make_pair(st, en));
    }
  }
  pair<long long, char> fr = v2[0];
  pair<long long, char> ls = v2.back();
  long long ans = 0;
  for (int i = 0; i < A.size(); i++) {
    int st = A[i].first;
    int en = A[i].second;
    if (st - 1 >= 0) {
      if (!(v1[st - 1].first >= fr.first && v1[st - 1].second == fr.second))
        continue;
    } else
      continue;
    if (en + 1 < v1.size()) {
      if (!(v1[en + 1].first >= ls.first && v1[en + 1].second == ls.second))
        continue;
    } else
      continue;
    ans++;
  }
  printf("%lld\n", ans);
  return 0;
}
