#include <bits/stdc++.h>
using namespace std;
struct RollingHash {
  static const long long mo0 = 1000000007, mo1 = 1000000009;
  static long long mul0, mul1;
  static const long long add0 = 1000010007, add1 = 1003333331;
  static vector<long long> pmo[2];
  vector<long long> s;
  int l;
  vector<long long> hash_[2];
  void init(vector<long long> s) {
    this->s = s;
    l = s.size();
    int i, j;
    hash_[0] = hash_[1] = vector<long long>(1, 0);
    if (!mul0)
      mul0 = 10009 + (((long long)&mul0) >> 5) % 259,
      mul1 = 10007 + (((long long)&mul1) >> 5) % 257;
    if (pmo[0].empty()) pmo[0].push_back(1), pmo[1].push_back(1);
    for (int i = 0; i < l; i++)
      hash_[0].push_back((hash_[0].back() * mul0 + add0 + s[i]) % mo0);
    for (int i = 0; i < l; i++)
      hash_[1].push_back((hash_[1].back() * mul1 + add1 + s[i]) % mo1);
  }
  pair<long long, long long> hash(int l, int r) {
    if (l > r) return make_pair(0, 0);
    while (pmo[0].size() < r + 2)
      pmo[0].push_back(pmo[0].back() * mul0 % mo0),
          pmo[1].push_back(pmo[1].back() * mul1 % mo1);
    return make_pair(
        (hash_[0][r + 1] + (mo0 - hash_[0][l] * pmo[0][r + 1 - l] % mo0)) % mo0,
        (hash_[1][r + 1] + (mo1 - hash_[1][l] * pmo[1][r + 1 - l] % mo1)) %
            mo1);
  }
};
vector<long long> RollingHash::pmo[2];
long long RollingHash::mul0, RollingHash::mul1;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<char, long long> > a;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    long long l = 0;
    for (int i = 0; i < s.length() - 2; i++) l = l * 10 + (s[i] - '0');
    char c = s[s.length() - 1];
    if (i == 0)
      a.push_back(pair<char, long long>(c, l));
    else {
      if (a[a.size() - 1].first == c)
        a[a.size() - 1].second += l;
      else
        a.push_back(pair<char, long long>(c, l));
    }
  }
  vector<pair<char, long long> > b;
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    long long l = 0;
    for (int i = 0; i < s.length() - 2; i++) l = l * 10 + (s[i] - '0');
    char c = s[s.length() - 1];
    if (i == 0)
      b.push_back(pair<char, long long>(c, l));
    else {
      if (b[b.size() - 1].first == c)
        b[b.size() - 1].second += l;
      else
        b.push_back(pair<char, long long>(c, l));
    }
  }
  n = a.size();
  m = b.size();
  long long ans = 0;
  if (m == 1) {
    for (int i = 0; i < n; i++)
      if (a[i].first == b[0].first)
        ans += max(0LL, a[i].second - b[0].second + 1);
  } else {
    RollingHash ahash;
    vector<long long> vec(n);
    for (int i = 0; i < n; i++) vec[i] = a[i].first - 'a' + a[i].second * 100;
    ahash.init(vec);
    RollingHash bhash;
    vec.resize(m);
    for (int i = 0; i < m; i++) vec[i] = b[i].first - 'a' + b[i].second * 100;
    bhash.init(vec);
    for (int i = 0; i < n - m + 1; i++) {
      if (m != 2) {
        auto ah = ahash.hash(i + 1, i + m - 2);
        auto bh = bhash.hash(1, m - 2);
        if (ah != bh) continue;
      }
      if (a[i].first != b[0].first) continue;
      if (a[i].second < b[0].second) continue;
      if (a[i + m - 1].first != b[m - 1].first) continue;
      if (a[i + m - 1].second < b[m - 1].second) continue;
      ans++;
    }
  }
  cout << ans << endl;
}
