#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<pair<long long, char> > s, t;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int a;
    char b;
    scanf("%d-%c", &a, &b);
    if (!s.empty() && s[s.size() - 1].second == b)
      s[s.size() - 1].first += a;
    else
      s.push_back(make_pair(a, b));
  }
  for (int i = 0; i < m; ++i) {
    int a;
    char b;
    scanf("%d-%c", &a, &b);
    if (!t.empty() && t[t.size() - 1].second == b)
      t[t.size() - 1].first += a;
    else
      t.push_back(make_pair(a, b));
  }
  long long ans = 0;
  if (t.size() == 1) {
    for (int i = 0; i < s.size(); ++i)
      if (s[i].second == t[0].second && t[0].first <= s[i].first)
        ans += s[i].first - t[0].first + 1;
  } else if (t.size() == 2) {
    for (int i = 1; i < s.size(); ++i)
      if (s[i - 1].second == t[0].second && t[0].first <= s[i - 1].first &&
          s[i].second == t[1].second && t[1].first <= s[i].first)
        ++ans;
  } else {
    vector<pair<long long, char> > new_t(t.begin() + 1, t.end() - 1);
    vector<int> f(new_t.size() + 1, 0);
    for (int i = 1; i < new_t.size(); ++i) {
      int j = f[i];
      while (j && new_t[i] != new_t[j]) j = f[j];
      f[i + 1] = new_t[i] == new_t[j] ? j + 1 : 0;
    }
    int j = 0;
    for (int i = 0; i < s.size(); ++i) {
      while (j && new_t[j] != s[i]) j = f[j];
      if (new_t[j] == s[i]) ++j;
      if (j == new_t.size()) {
        int st = i - new_t.size(), ed = i + 1;
        if (st >= 0 && s[st].second == t[0].second &&
            s[st].first >= t[0].first && ed < s.size() &&
            s[ed].second == t[t.size() - 1].second &&
            s[ed].first >= t[t.size() - 1].first)
          ++ans;
        j = f[j];
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
