#include <bits/stdc++.h>
long long mod = 1e9 + 7;
long long mod1 = 1e9 + 6;
long long mod2 = 2LL;
long long maxm = 1e18;
using namespace std;
long long powmod(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return ans;
}
long long modinv(long long a, long long c) {
  if (a == 0)
    return 1LL;
  else
    return powmod(a, c - 2, c);
}
long long MOD(long long a) { return (a % mod + 2 * mod) % mod; }
int get(char c) { return (int)(c)-96; }
long long cum[200002];
long long len[200002];
long long patv;
long long plen;
int ltxt;
int lpat;
int main() {
  int t, i, k, j;
  long long constvalue = modinv(25, mod);
  int test, a, b, c, q, p, out;
  int rt, n, m;
  char c1, c2;
  t = 1;
  for (test = 1; test <= t; ++test) {
    cin >> n >> m;
    vector<pair<char, long long> > txt;
    vector<pair<char, long long> > pat;
    for (i = 1; i <= n; ++i) {
      scanf("%d%c%c", &a, &c1, &c2);
      if (i == 1)
        txt.push_back(make_pair(c2, a));
      else {
        int lst = txt.size() - 1;
        if (txt[lst].first != c2)
          txt.push_back(make_pair(c2, a));
        else
          txt[lst].second += a;
      }
    }
    ltxt = txt.size() - 1;
    for (i = 1; i <= m; ++i) {
      scanf("%d%c%c", &a, &c1, &c2);
      if (i == 1)
        pat.push_back(make_pair(c2, a));
      else {
        int lst = pat.size() - 1;
        if (pat[lst].first != c2)
          pat.push_back(make_pair(c2, a));
        else
          pat[lst].second += a;
      }
    }
    lpat = pat.size() - 1;
    cum[0] = powmod(26, txt[0].second, mod) - 1;
    cum[0] *= constvalue;
    len[0] = txt[0].second;
    cum[0] = MOD(cum[0]);
    int ascii = get(txt[0].first);
    cum[0] *= ascii;
    cum[0] = MOD(cum[0]);
    for (i = 1; i <= ltxt; ++i) {
      cum[i] = cum[i - 1] * powmod(26, txt[i].second, mod);
      cum[i] = MOD(cum[i]);
      long long added = powmod(26, txt[i].second, mod) - 1;
      added *= constvalue;
      added = MOD(added);
      ascii = get(txt[i].first);
      added *= ascii;
      added = MOD(added);
      cum[i] += added;
      cum[i] = MOD(cum[i]);
      len[i] = len[i - 1] + txt[i].second;
    }
    patv = 0;
    for (i = 1; i <= lpat - 1; ++i) {
      patv = patv * powmod(26, pat[i].second, mod);
      patv = MOD(patv);
      long long added = powmod(26, pat[i].second, mod) - 1;
      added *= constvalue;
      added = MOD(added);
      ascii = get(pat[i].first);
      added *= ascii;
      added = MOD(added);
      patv += added;
      patv = MOD(patv);
    }
    long long ans = 0;
    for (i = lpat; i <= ltxt; ++i) {
      if (lpat == 0) {
        if (txt[i].first == pat[0].first) {
          ans += max(0LL, txt[i].second - pat[0].second + 1);
        }
      } else {
        int st = i - lpat + 1;
        int en = i - 1;
        long long value = 0;
        if (st <= en) {
          value =
              cum[en] - cum[st - 1] * powmod(26, len[en] - len[st - 1], mod);
          value = MOD(value);
        }
        if ((txt[st - 1].first == pat[0].first) &&
            (txt[en + 1].first == pat[lpat].first)) {
          if (value == patv) {
            if ((txt[st - 1].second >= pat[0].second) &&
                (txt[en + 1].second >= pat[lpat].second))
              ans += 1;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
