#include <bits/stdc++.h>
using namespace std;
void fre() {
  freopen("c://test//input.in", "r", stdin);
  freopen("c://test//output.out", "w", stdout);
}
template <class T1, class T2>
inline void gmax(T1 &a, T2 b) {
  if (b > a) a = b;
}
template <class T1, class T2>
inline void gmin(T1 &a, T2 b) {
  if (b < a) a = b;
}
const int N = 2e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int nn, mm;
int n, m;
pair<long long, char> a[N], b[N], first, last;
int nxt[N];
void getnxt() {
  int j = 1;
  nxt[2] = 1;
  for (int i = 3; i < m; ++i) {
    while (j > 1 && b[j + 1] != b[i]) j = nxt[j];
    if (b[j + 1] == b[i]) ++j;
    nxt[i] = j;
  }
}
int kmp() {
  int ret = 0;
  int len = m - 2;
  int j = 1;
  for (int i = 2; i < n; ++i) {
    while (j > 1 && b[j + 1] != a[i]) j = nxt[j];
    if (b[j + 1] == a[i]) ++j;
    if (j - 1 == len && a[i + 1].first >= last.first &&
        a[i + 1].second == last.second && a[i - len].first >= first.first &&
        a[i - len].second == first.second)
      ++ret;
  }
  return ret;
}
int main() {
  while (~scanf("%d%d", &nn, &mm)) {
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    n = 0;
    m = 0;
    for (int i = 1; i <= nn; ++i) {
      int x;
      char ch;
      scanf("%d-%c", &x, &ch);
      if (ch == a[n].second)
        a[n].first += x;
      else
        a[++n] = make_pair(x, ch);
    }
    for (int j = 1; j <= mm; ++j) {
      int x;
      char ch;
      scanf("%d-%c", &x, &ch);
      if (ch == b[m].second)
        b[m].first += x;
      else
        b[++m] = make_pair(x, ch);
    }
    long long ans = 0;
    if (m == 1) {
      for (int i = 1; i <= n; ++i)
        if (a[i].first >= b[1].first && a[i].second == b[1].second) {
          ans += a[i].first - b[1].first + 1;
        }
    } else if (m == 2) {
      for (int i = 1; i < n; ++i)
        if (a[i].first >= b[1].first && a[i].second == b[1].second &&
            a[i + 1].first >= b[2].first && a[i + 1].second == b[2].second) {
          ++ans;
        }
    } else {
      first = b[1];
      b[1] = make_pair(0, 0);
      last = b[m];
      b[m] = make_pair(0, 0);
      getnxt();
      ans = kmp();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
