#include <bits/stdc++.h>
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
vector<int> makeTable(const vector<pair<char, long long> >& s) {
  int n = s.size();
  vector<int> ret(n + 1);
  ret[0] = -1;
  int j = -1;
  for (int i = 0; i < n; i++) {
    while (j >= 0 && s[i] != s[j]) j = ret[j];
    ret[i + 1] = ++j;
  }
  return ret;
}
vector<int> kmp(const vector<pair<char, long long> >& str,
                const vector<pair<char, long long> >& word) {
  vector<int> table = makeTable(word), ret;
  int m = 0, i = 0, n = str.size();
  while (m + i < n) {
    if (word[i] == str[m + i]) {
      if (++i == (int)(word.size())) {
        ret.push_back(m);
        m = m + i - table[i];
        i = table[i];
      }
    } else {
      m = m + i - table[i];
      if (i > 0) i = table[i];
    }
  }
  return ret;
}
void create(int n, vector<pair<char, long long> >& S) {
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    long long l = stoll(s.substr(0, s.size() - 2));
    char c = s.back();
    if (S.empty() || S.back().first != c)
      S.emplace_back(c, l);
    else
      S.back().second += l;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<pair<char, long long> > S, T;
  create(n, T);
  create(m, S);
  long long ans = 0;
  int tsize = T.size();
  if (S.size() == 1) {
    char c = S[0].first;
    long long l = S[0].second;
    for (auto p : T) {
      if (p.first == c) {
        ans += max(0ll, p.second - l + 1);
      }
    }
  } else if (S.size() == 2) {
    for (int i = 0; i < tsize - 1; i++) {
      bool ok = true;
      for (int j = 0; j < 2; j++) {
        if (T[i + j].first != S[j].first || T[i + j].second < S[j].second)
          ok = false;
      }
      if (ok) ans++;
    }
  } else {
    pair<char, long long> start = S[0], end = S.back();
    vector<pair<char, long long> > s;
    int ssize = S.size();
    for (int i = 1; i < ssize - 1; i++) s.push_back(S[i]);
    vector<int> memo = kmp(T, s);
    for (int el : memo) {
      if (el == 0 || el + 1 == tsize) continue;
      if (T[el - 1].first != start.first || T[el - 1].second < start.second)
        continue;
      if (T[el + ssize - 2].first != end.first ||
          T[el + ssize - 2].second < end.second)
        continue;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
