#include <bits/stdc++.h>
using namespace std;
template <typename G1, typename G2 = G1, typename G3 = G1>
struct triple {
  G1 first;
  G2 second;
  G3 T;
};
pair<long long, char> read() {
  string s;
  cin >> s;
  int x = 0;
  for (int i = 0; i < s.size() - 2; i++) x = 10 * x + (s[i] - '0');
  return {x, s.back()};
}
vector<int> CPF(vector<pair<long long, char> > &pattern) {
  vector<int> p(pattern.size());
  p[0] = 0;
  int k = 0;
  for (int i = 1; i < pattern.size(); i++) {
    while (k > 0 && pattern[k] != pattern[i]) k = p[k - 1];
    if (pattern[k] == pattern[i]) ++k;
    p[i] = k;
  }
  return move(p);
}
vector<int> KMP(vector<pair<long long, char> > &text,
                vector<pair<long long, char> > &pattern) {
  auto p = CPF(pattern);
  vector<int> ans;
  int k = 0;
  for (int i = 0; i < text.size(); i++) {
    while (k > 0 && pattern[k] != text[i]) k = p[k - 1];
    if (pattern[k] == text[i]) ++k;
    if (k == pattern.size()) {
      ans.push_back(i - k + 1);
      k = p[k - 1];
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<long long, char> > v, w;
  for (int i = 0; i < n; i++) {
    pair<long long, char> temp = read();
    if (!v.empty() && v.back().second == temp.second)
      v.back().first += temp.first;
    else
      v.push_back(temp);
  }
  for (int i = 0; i < m; i++) {
    pair<long long, char> temp = read();
    if (!w.empty() && w.back().second == temp.second)
      w.back().first += temp.first;
    else
      w.push_back(temp);
  }
  long long ans = 0;
  if (w.size() == 1) {
    for (auto &i : v)
      if (i.second == w[0].second && i.first >= w[0].first)
        ans += i.first - w[0].first + 1;
  } else if (w.size() == 2) {
    for (int i = 0; i < v.size() - 1; i++)
      if (v[i].second == w[0].second && v[i].first >= w[0].first &&
          v[i + 1].second == w[1].second && v[i + 1].first >= w[1].first)
        ++ans;
  } else {
    vector<pair<long long, char> > p;
    for (int i = 1; i < w.size() - 1; i++) p.push_back(w[i]);
    int k = p.size();
    auto oc = KMP(v, p);
    for (auto i : oc) {
      if (i > 0 && v[i - 1].second == w[0].second &&
          v[i - 1].first >= w[0].first && i + k < v.size() &&
          v[i + k].second == w.back().second &&
          v[i + k].first >= w.back().first)
        ++ans;
    }
  }
  cout << ans << '\n';
  return 0;
}
