#include <bits/stdc++.h>
using namespace std;
const long long int N = 4e5 + 5, inf = 1e9 + 7;
int a, b, fail[N];
long long int res = 0;
pair<long long int, char> P1[N], P2[N], P3[N], x, y;
bool Ok(pair<long long int, char> P1, pair<long long int, char> P2) {
  if (P1.second != P2.second || P1.first != P2.first) return false;
  return true;
}
void kmp() {
  for (int i = 1; i < b - 1; i++) P3[i - 1] = P2[i];
  x = P2[0], y = P2[b - 1];
  fail[0] = 0;
  int j = 0;
  for (int i = 1; i < b - 2; i++) {
    while (j && !Ok(P3[i], P3[j])) j = fail[j - 1];
    if (Ok(P3[i], P3[j])) j++;
    fail[i] = j;
  }
  j = 0;
  for (int i = 1; i < a - 1; i++) {
    while (j && !Ok(P1[i], P3[j])) j = fail[j - 1];
    if (Ok(P1[i], P3[j])) j++;
    if (j == b - 2) {
      res += (x.first <= P1[i - j].first && y.first <= P1[i + 1].first &&
              x.second == P1[i - j].second && y.second == P1[i + 1].second);
      j = fail[j - 1];
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int sz = s.size(), j, num = 0;
    for (j = 0; j < sz && s[j] != '-'; j++) num = num * 10 + (s[j] - '0');
    j++;
    if (a && P1[a - 1].second == s[sz - 1])
      P1[a - 1].first += num;
    else
      P1[a++] = make_pair(num, s[sz - 1]);
  }
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    int sz = s.size(), j, num = 0;
    for (j = 0; j < sz && s[j] != '-'; j++) num = num * 10 + (s[j] - '0');
    j++;
    if (b && P2[b - 1].second == s[sz - 1])
      P2[b - 1].first += num;
    else
      P2[b++] = make_pair(num, s[sz - 1]);
  }
  if (b == 1) {
    for (int i = 0; i < a; i++) {
      if (P2[0].second == P1[i].second)
        res += max(0LL, P1[i].first - P2[0].first + 1);
    }
    cout << res << "\n", exit(0);
  }
  if (b == 2) {
    for (int i = 0; i < a - 1; i++) {
      if (P2[0].second == P1[i].second && P2[1].second == P1[i + 1].second)
        res += (P1[i].first >= P2[0].first && P1[i + 1].first >= P2[1].first);
    }
    cout << res << "\n", exit(0);
  }
  kmp();
  cout << res << "\n";
}
