#include <bits/stdc++.h>
using namespace std;
const int BASE = 257;
const int B = BASE;
const int MAXN = 200100;
const int MOD1 = 1000000007;
const int MOD2 = 1000000009;
const int MAXL = 1100100;
long long n, m, l, sol;
char c;
long long N, M;
pair<long long, char> s[MAXN], t[MAXN];
long long ps[MAXN], pt[MAXN];
long long hs1[MAXN], hs2[MAXN], ht1[MAXN], ht2[MAXN];
long long log_pow(long long b, long long e, long long MOD) {
  long long r = 1;
  while (e) {
    if (e & 1) r = r * b % MOD;
    b = b * b % MOD;
    e >>= 1;
  }
  return r;
}
long long inv_mod(long long x, long long MODP) {
  return log_pow(x, MODP - 2, MODP);
}
long long hash_of_block(pair<long long, char> x, long long MODP) {
  return (log_pow(B, x.first, MODP) - 1) * inv_mod(B - 1, MODP) % MODP *
         x.second % MODP;
}
void init_hash(int X, pair<long long, char> x[MAXN], long long px[MAXN],
               long long hx1[MAXN], long long hx2[MAXN]) {
  px[0] = 0;
  for (int i = 1; i <= X; i++) {
    px[i] = px[i - 1] + x[i - 1].first;
  }
  hx1[0] = 0;
  hx2[0] = 0;
  for (int i = 1; i <= X; i++) {
    hx1[i] = (hx1[i - 1] + log_pow(B, px[i - 1], MOD1) % MOD1 *
                               hash_of_block(x[i - 1], MOD1)) %
             MOD1;
    hx2[i] = (hx2[i - 1] + log_pow(B, px[i - 1], MOD2) % MOD2 *
                               hash_of_block(x[i - 1], MOD2)) %
             MOD2;
  }
}
pair<long long, long long> hash_of(long long hx1[MAXN], long long hx2[MAXN],
                                   long long px[MAXN], int lo, int hi) {
  if (lo > hi) return pair<long long, long long>(0, 0);
  lo++, hi++;
  long long h1 = (hx1[hi] - hx1[lo - 1] + MOD1) % MOD1 *
                 inv_mod(log_pow(B, px[lo - 1], MOD1), MOD1) % MOD1;
  long long h2 = (hx2[hi] - hx2[lo - 1] + MOD2) % MOD2 *
                 inv_mod(log_pow(B, px[lo - 1], MOD2), MOD2) % MOD2;
  return pair<long long, long long>(h1, h2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> l;
    cin >> c;
    cin >> c;
    t[N] = pair<int, int>(l, c);
    while (N > 0 && t[N].second == t[N - 1].second) {
      t[N - 1].first += t[N].first;
      N--;
    }
    N++;
  }
  for (int i = 0; i < m; i++) {
    cin >> l;
    cin >> c;
    cin >> c;
    s[M] = pair<int, int>(l, c);
    while (M > 0 && s[M].second == s[M - 1].second) {
      s[M - 1].first += s[M].first;
      M--;
    }
    M++;
  }
  init_hash(N, t, pt, ht1, ht2);
  init_hash(M, s, ps, hs1, hs2);
  sol = 0;
  for (int i = 0; i <= N - M; i++) {
    int j = i + M - 1;
    if (s[0].second == t[i].second && s[0].first <= t[i].first &&
        s[M - 1].second == t[j].second && s[M - 1].first <= t[j].first &&
        hash_of(hs1, hs2, ps, 1, M - 2) == hash_of(ht1, ht2, pt, i + 1, j - 1))
      sol++;
    if (M == 1 && s[0].second == t[i].second && s[0].first <= t[i].first)
      sol += t[i].first - s[0].first;
  }
  cout << sol << endl;
}
