#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n, m, ans, p[N];
pair<long long, long long> a[N], b[N];
char s[50];
void doit(pair<long long, long long> *a, long long &n) {
  static pair<long long, long long> b[N];
  long long cnt = 0;
  for (long long i = 1; i <= n; ++i) {
    if (a[i].second != a[i - 1].second)
      b[++cnt] = a[i];
    else
      b[cnt].first += a[i].first;
  }
  n = cnt;
  for (long long i = 1; i <= n; ++i) a[i] = b[i];
  a[n + 1] = pair<long long, long long>(0, 0);
}
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    long long len = strlen(s + 1);
    a[i].second = s[len] - 'a' + 1;
    for (long long j = 1; j < len - 1; ++j)
      a[i].first = a[i].first * 10 + (s[j] ^ '0');
  }
  for (long long i = 1; i <= m; ++i) {
    scanf("%s", s + 1);
    long long len = strlen(s + 1);
    b[i].second = s[len] - 'a' + 1;
    for (long long j = 1; j < len - 1; ++j)
      b[i].first = b[i].first * 10 + (s[j] ^ '0');
  }
  doit(a, n);
  doit(b, m);
  if (m == 1) {
    for (long long i = 1; i <= n; ++i)
      if (a[i].second == b[1].second && a[i].first >= b[1].first)
        ans += a[i].first - b[1].first + 1;
    printf("%lld\n", ans);
  } else if (m < 5) {
    for (long long pos = 1; pos + m - 1 <= n; ++pos) {
      long long jud = 1;
      if (a[pos].second != b[1].second || a[pos].first < b[1].first) continue;
      if (a[pos + m - 1].second != b[m].second ||
          a[pos + m - 1].first < b[m].first)
        continue;
      for (long long i = 2; i < m; ++i)
        if (a[pos + i - 1] != b[i]) {
          jud = 0;
          break;
        }
      ans += jud;
    }
    printf("%lld\n", ans);
  } else {
    pair<long long, long long> A = b[1], B = b[m];
    for (long long i = 1; i < m - 1; ++i) b[i] = b[i + 1];
    m -= 2;
    p[1] = 0;
    for (long long i = 2, idx; i <= m; ++i) {
      idx = p[i - 1];
      for (; idx && b[idx + 1] != b[i]; idx = p[idx])
        ;
      p[i] = idx + (b[idx + 1] == b[i]);
    }
    for (long long i = 2, pos = 0; i < n; ++i) {
      for (; pos && b[pos + 1] != a[i]; pos = p[pos])
        ;
      pos += (b[pos + 1] == a[i]);
      if (pos == m) {
        long long st = i - m;
        if (a[st].second != A.second || a[st].first < A.first) continue;
        if (a[st + m + 1].second != B.second || a[st + m + 1].first < B.first)
          continue;
        ++ans;
        pos = p[pos];
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
