#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
template <typename T>
inline void read(T &WOW) {
  T x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  WOW = flag * x;
}
int n, m, slen, tlen, sslen, nxt[MAXN];
struct data {
  int c;
  long long l;
  data() {}
  data(int _c, long long _l) : c(_c), l(_l) {}
  bool operator==(const data &rhs) const { return (c == rhs.c && l == rhs.l); }
  bool operator!=(const data &rhs) const { return (c != rhs.c || l != rhs.l); }
} s[MAXN], t[MAXN], ss[MAXN];
void init() {
  read(n);
  read(m);
  for (int i = 1; i <= n; ++i) {
    int l;
    read(l);
    char ch = getchar();
    int c = ch - 'a';
    if (tlen && c == t[tlen].c) {
      t[tlen].l += l;
    } else {
      t[++tlen] = data(c, l);
    }
  }
  for (int i = 1; i <= m; ++i) {
    int l;
    read(l);
    char ch = getchar();
    int c = ch - 'a';
    if (slen && c == s[slen].c) {
      s[slen].l += l;
    } else {
      s[++slen] = data(c, l);
    }
  }
}
void solve() {
  if (slen == 1) {
    long long ans = 0;
    for (int i = 1; i <= tlen; ++i) {
      if (s[1].c == t[i].c && s[1].l <= t[i].l) {
        ans += (t[i].l - s[1].l + 1);
      }
    }
    printf("%lld\n", ans);
    return;
  }
  if (slen == 2) {
    long long ans = 0;
    for (int i = 1; i < tlen; ++i) {
      if (s[1].c == t[i].c && s[1].l <= t[i].l && s[2].c == t[i + 1].c &&
          s[2].l <= t[i + 1].l) {
        ++ans;
      }
    }
    printf("%lld\n", ans);
    return;
  }
  for (int i = 2; i < slen; ++i) {
    ss[++sslen] = s[i];
  }
  for (int i = 2, k = 0; i <= sslen; ++i) {
    while (k && ss[k + 1] != ss[i]) {
      k = nxt[k];
    }
    if (s[k + 1] == s[i]) {
      nxt[i] = ++k;
    }
  }
  int ans = 0;
  for (int i = 1, k = 0; i <= tlen; ++i) {
    while (k && ss[k + 1] != t[i]) {
      k = nxt[k];
    }
    if (ss[k + 1] == t[i]) {
      ++k;
    }
    if (k == sslen) {
      if (i - sslen + 1 > 1 && i < tlen) {
        int l = i - sslen + 1, r = i;
        if (s[1].c == t[l - 1].c && s[1].l <= t[l - 1].l &&
            s[slen].c == t[r + 1].c && s[slen].l <= t[r + 1].l) {
          ++ans;
        }
      }
      k = nxt[k];
    }
  }
  printf("%d\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}
