#include <bits/stdc++.h>
using namespace std;
pair<long long, char> ch(string str) {
  long long n = 0;
  for (long long i = 0; i < str.size() - 2; i++) n *= 10, n += str[i] - 48;
  return {n, str.back()};
}
bool can(pair<long long, char> a, pair<long long, char> b) {
  return (a.second == b.second && a.first >= b.first);
}
vector<pair<long long, char> > make(vector<string> ve) {
  vector<pair<long long, char> > ret;
  ret.push_back(ch(ve[0]));
  for (long long i = 1; i < ve.size(); i++) {
    auto t = ch(ve[i]);
    if (ret.back().second == t.second)
      ret.back().first += t.first;
    else
      ret.push_back(t);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<string> a1(n), b1(m);
  for (auto &i : a1) cin >> i;
  for (auto &i : b1) cin >> i;
  auto a = make(a1), b = make(b1);
  n = a.size();
  m = b.size();
  if (m == 1) {
    long long res = 0;
    auto t = b[0];
    for (auto i : a) {
      if (t.second == i.second) res += max(0ll, i.first - t.first + 1);
    }
    cout << res;
  } else {
    vector<pair<long long, char> > ve;
    long long res = 0;
    for (auto i : b) ve.push_back(i);
    ve.push_back({1ll, '#'});
    for (auto i : a) ve.push_back(i);
    long long pi[ve.size()];
    pi[0] = 0;
    for (long long i = 1; i < ve.size(); i++) {
      long long j = pi[i - 1];
      if (j == m - 1 && can(ve[i], ve[m - 1])) res++;
      while (j > 0 && ve[i] != ve[j]) j = pi[j - 1];
      if (ve[i] == ve[j] || (j == 0 && can(ve[i], ve[j]))) j++;
      pi[i] = j;
    }
    cout << res;
  }
  return 0;
}
