#include <bits/stdc++.h>
using namespace std;
long long in() {
  long long re = 0, f = 1;
  char x = getchar();
  while (x < '0' || x > '9') {
    if (x == '-') f = -1;
    x = getchar();
  }
  while (x >= '0' && x <= '9') re = re * 10 + x - 48, x = getchar();
  return re * f;
}
void out(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (!x) {
    putchar('0');
    return;
  }
  char z[21], ct = 0;
  while (x) {
    z[++ct] = x % 10 + 48, x /= 10;
  }
  while (ct) {
    putchar(z[ct]);
    --ct;
  }
}
const long long mod1 = 1e9 + 7;
const long long mod2 = 998244353;
const long long base1 = 3001;
const long long base2 = 2333;
long long upd(long long a, long long b) { return a < 0 ? a + b : a; }
struct ull {
  long long x, y;
  ull operator-(const ull &b) const {
    return (ull){upd(x - b.x, mod1), upd(y - b.y, mod2)};
  }
  ull operator+(const long long &b) const {
    return (ull){(x + b) % mod1, (y + b) % mod2};
  }
  ull operator+(const ull &b) const {
    return (ull){(x + b.x) % mod1, (y + b.y) % mod2};
  }
  ull operator*(const ull &b) const {
    return (ull){(x * b.x) % mod1, (y * b.y) % mod2};
  }
  bool operator==(const ull &b) const { return x == b.x && y == b.y; }
  bool operator!=(const ull &b) const { return x != b.x || y != b.y; }
} base;
ull hs[1000010], pw[1000010];
ull geths(int l, int r) {
  int len = r - l + 1;
  ull re = hs[r] - hs[l - 1] * pw[len];
  return re;
}
long long ls[200010], lt[200010];
char cs[200010], ct[200010];
long long S[500000], T[500000];
int n, m;
int main() {
  n = in();
  m = in();
  for (int i = 1; i <= n; ++i) {
    scanf("%d-%c", &ls[i], &cs[i]);
    if (cs[i] == cs[i - 1]) {
      ls[i - 1] += ls[i];
      --i;
      --n;
    }
  }
  for (int i = 1; i <= m; ++i) {
    scanf("%d-%c", &lt[i], &ct[i]);
    if (ct[i] == ct[i - 1]) {
      lt[i - 1] += lt[i];
      --i;
      --m;
    }
  }
  if (m == 1) {
    char c = ct[1];
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
      if (cs[i] == c) ans = ans + max(0ll, ls[i] - lt[1] + 1);
    cout << ans << "\n";
    return 0;
  }
  base = (ull){base1, base2};
  int cnt1 = lt[1], cnt2 = lt[m], ch = ct[m];
  for (int i = 1; i <= n; ++i) S[i * 2 - 1] = ls[i], S[i * 2] = cs[i];
  T[1] = ct[1];
  for (int i = 2; i < m; ++i) T[i * 2 - 2] = lt[i], T[i * 2 - 1] = ct[i];
  int tlen = m * 2 - 3;
  ull hT = (ull){0, 0};
  for (int i = 1; i <= tlen; ++i) hT = hT * base + (long long)T[i];
  pw[0].x = pw[0].y = 1;
  for (int ed = max(tlen, n * 2), i = 1; i <= ed; ++i) pw[i] = pw[i - 1] * base;
  for (int i = 1; i <= n * 2; ++i) hs[i] = hs[i - 1] * base + (long long)S[i];
  long long ans = 0;
  for (int i = 2; i + tlen + 1 <= n * 2; i += 2) {
    int l = i, r = i + tlen - 1;
    ull hS = geths(l, r);
    if (hS == hT) {
      if (S[l - 1] < cnt1) continue;
      if (S[r + 1] < cnt2) continue;
      if (S[r + 2] != ch) continue;
      ++ans;
    }
  }
  cout << ans << "\n";
}
