#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<long long, char> > a, b;
int dfa[200005];
pair<long long, char> parse(string &s) {
  long long res = 0;
  for (char c : s)
    if (c == '-')
      break;
    else
      res = res * 10 + (c - '0');
  return {res, s.back()};
}
void DFA() {
  dfa[1] = 1;
  for (int i = 2, k = 1; i < b.size() - 1; ++i) {
    k = dfa[i - 1];
    while (k > 1 && b[i] != b[k]) k = dfa[k - 1];
    if (b[k] == b[i]) ++k;
    dfa[i] = k;
  }
}
int KMP() {
  int ret = 0;
  for (int i = 1, k = 1; i < a.size() - 1; ++i) {
    while (k > 1 && a[i] != b[k]) k = dfa[k - 1];
    if (b[k] == a[i]) ++k;
    if (k == b.size() - 1) {
      if (a[i + 1].second == b.back().second &&
          a[i + 1].first >= b.back().first &&
          a[i - b.size() + 2].second == b[0].second &&
          a[i - b.size() + 2].first >= b[0].first) {
        ret++;
      }
      k = dfa[k - 1];
    }
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  string s;
  while (n--) {
    cin >> s;
    pair<long long, char> res = parse(s);
    if (!a.empty() && a.back().second == res.second)
      a.back().first += res.first;
    else
      a.push_back(res);
  }
  while (m--) {
    cin >> s;
    pair<long long, char> res = parse(s);
    if (!b.empty() && b.back().second == res.second)
      b.back().first += res.first;
    else
      b.push_back(res);
  }
  long long ans = 0;
  if (b.size() == 1) {
    for (int i = 0; i < a.size(); ++i) {
      if (b[0].second == a[i].second && b[0].first <= a[i].first) {
        ans += a[i].first - b[0].first + 1;
      }
    }
  } else if (b.size() == 2) {
    for (int i = 0; i < a.size() - 1; ++i) {
      if (b[0].second == a[i].second && b[0].first <= a[i].first &&
          b[1].second == a[i + 1].second && b[1].first <= a[i + 1].first) {
        ans++;
      }
    }
  } else {
    DFA();
    ans = KMP();
  }
  cout << ans;
  return 0;
}
