#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
struct node {
  long long l;
  char c;
  friend bool operator==(const node &a, const node &b) {
    return a.l == b.l && a.c == b.c;
  }
  friend bool operator!=(const node &a, const node &b) {
    return a.c == b.c && a.l >= b.l;
  }
} s1[N], s2[N], st, en;
long long ans;
int n, m, cnt, nex[N];
void solve() {
  st = s2[1];
  en = s2[m];
  for (int i = 1; i <= m - 2; i++) s2[i] = s2[i + 1];
  m = m - 2;
  s2[m + 1] = s2[0];
  for (int i = 2, j; i <= m; i++) {
    j = nex[i - 1];
    while (j && !(s2[i] == s2[j + 1])) j = nex[j];
    if (s2[i] == s2[j + 1]) j++;
    nex[i] = j;
  }
  for (int i = 1, j = 0; i <= n; i++) {
    while (j && !(s1[i] == s2[j + 1])) j = nex[j];
    if (s1[i] == s2[j + 1]) j++;
    if (j == m) {
      if (i <= n && s1[i + 1] != en && i >= m && s1[i - m] != st) ans++;
      j = nex[j];
    }
  }
  printf("%I64d\n", ans);
}
void solve1() {
  for (int i = 1; i <= n; i++)
    if (s1[i] != s2[1]) ans += s1[i].l - s2[1].l + 1;
  printf("%I64d\n", ans);
}
void solve2() {
  for (int i = 1; i < n; i++)
    if (s1[i] != s2[1] && s1[i + 1] != s2[2]) ans++;
  printf("%I64d\n", ans);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &s1[++cnt].l);
    s1[cnt].c = getchar();
    s1[cnt].c = getchar();
    if (cnt && s1[cnt - 1].c == s1[cnt].c) {
      s1[cnt - 1].l += s1[cnt].l;
      cnt--;
    }
  }
  n = cnt;
  cnt = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%I64d", &s2[++cnt].l);
    s2[cnt].c = getchar();
    s2[cnt].c = getchar();
    if (cnt && s2[cnt - 1].c == s2[cnt].c) {
      s2[cnt - 1].l += s2[cnt].l;
      cnt--;
    }
  }
  m = cnt;
  cnt = 0;
  if (m == 1)
    solve1();
  else if (m == 2)
    solve2();
  else
    solve();
  return 0;
}
