#include <bits/stdc++.h>
using namespace std;
const int nmax = 200005;
int n, m, pi[nmax];
vector<pair<long long, char> > p, t, P, T, PP, TT;
int ANS = 0;
int Pn;
bool ok(pair<long long, char> aa, pair<long long, char> bb) {
  if (aa.second != bb.second) {
    return false;
  }
  if (aa.first == bb.first) {
    return true;
  }
  return false;
}
bool ok2(pair<long long, char> aa, pair<long long, char> bb) {
  if (aa.second != bb.second) {
    return false;
  }
  if (aa.first <= bb.first) {
    return true;
  }
  return false;
}
void prefix() {
  pi[1] = 0;
  for (int i = 2, q = 0; i < Pn; ++i) {
    while (q > 0 && !ok(PP[q + 1], PP[i])) {
      q = pi[q];
    }
    if (ok(PP[q + 1], PP[i])) ++q;
    pi[i] = q;
  }
}
void bagaKmp() {
  for (int i = 1, q = 0; i < TT.size(); ++i) {
    while (q > 0 && !ok(PP[q + 1], TT[i])) {
      q = pi[q];
    }
    if (ok(PP[q + 1], TT[i])) {
      ++q;
    }
    if (q == Pn - 2) {
      if (i + 1 < TT.size() && i - q > 0) {
        if (ok2(P[0], TT[i - q]) && ok2(P[P.size() - 1], TT[i + 1])) {
          ++ANS;
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  cin.get();
  for (int i = 1; i <= n; ++i) {
    int li;
    char c, lin;
    cin >> li >> lin >> c;
    t.push_back(make_pair(li, c));
  }
  for (int i = 1; i <= m; ++i) {
    int li;
    char c, lin;
    cin >> li >> lin >> c;
    p.push_back(make_pair(li, c));
  }
  t.push_back(make_pair(0, -1));
  long long current = t[0].first;
  for (int i = 0; i + 1 < t.size(); ++i) {
    if (t[i].second != t[i + 1].second) {
      T.push_back(make_pair(current, t[i].second));
      current = t[i + 1].first;
    } else {
      current += t[i + 1].first;
    }
  }
  p.push_back(make_pair(0, -1));
  current = p[0].first;
  for (int i = 0; i + 1 < p.size(); ++i) {
    if (p[i].second != p[i + 1].second) {
      P.push_back(make_pair(current, p[i].second));
      current = p[i + 1].first;
    } else {
      current += p[i + 1].first;
    }
  }
  long long ans = 0;
  if (P.size() == 1) {
    for (int i = 0; i < T.size(); ++i) {
      if (T[i].first >= P[0].first && T[i].second == P[0].second) {
        ans += T[i].first - P[0].first + 1;
      }
    }
    cout << ans << "\n";
    return 0;
  }
  if (P.size() == 2) {
    ans = 0;
    for (int i = 0; i < T.size() - 1; ++i) {
      if (T[i].first >= P[0].first && T[i].second == P[0].second &&
          T[i + 1].first >= P[1].first && T[i + 1].second == P[1].second) {
        ans++;
      }
    }
    cout << ans << "\n";
    return 0;
  }
  TT.push_back(make_pair(0, 0));
  PP.push_back(make_pair(0, 0));
  for (int i = 0; i < T.size(); ++i) {
    TT.push_back(T[i]);
  }
  for (int i = 1; i + 1 < P.size(); ++i) {
    PP.push_back(P[i]);
  }
  Pn = P.size();
  PP.push_back(make_pair(0, 'A'));
  prefix();
  bagaKmp();
  cout << ANS << "\n";
  return 0;
}
