#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
pair<long long, long long> a[maxn], b[maxn];
int sza, szb, T[maxn];
void inp(pair<long long, long long> *a, int n, int &sz) {
  sz = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int l = s.length();
    int ch = s[l - 1];
    s.resize(l - 2);
    stringstream ss;
    ss << s;
    int num;
    ss >> num;
    if (sz && a[sz - 1].first == ch)
      a[sz - 1].second += num;
    else {
      a[sz].first = ch;
      a[sz].second = num;
      sz++;
    }
  }
}
void init() {
  int i = 0, j = -1;
  T[0] = -1;
  while (i < szb - 2) {
    while (j >= 0 && b[j + 1] != b[i + 1]) j = T[j];
    i++;
    j++;
    T[i] = j;
  }
}
long long match() {
  long long cnt = 0;
  int i = 0, j = 0;
  while (i < sza - 2) {
    while (j >= 0 && b[j + 1] != a[i + 1]) j = T[j];
    i++;
    j++;
    if (j == szb - 2) {
      if (b[0].first == a[i - j].first && b[j + 1].first == a[i + 1].first)
        if (b[0].second <= a[i - j].second &&
            b[j + 1].second <= a[i + 1].second)
          cnt++;
      j = T[j];
    }
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  inp(a, n, sza);
  inp(b, m, szb);
  long long cnt = 0;
  if (szb == 1) {
    for (int i = 0; i < sza; i++)
      if (a[i].first == b[0].first && a[i].second >= b[0].second)
        cnt += a[i].second - b[0].second + 1;
  } else if (szb == 2) {
    for (int i = 0; i < sza - 1; i++)
      if (a[i].first == b[0].first && a[i].second >= b[0].second)
        if (a[i + 1].first == b[1].first && a[i + 1].second >= b[1].second)
          cnt++;
  } else {
    init();
    cnt = match();
  }
  cout << cnt << "\n";
  return 0;
}
