#include <bits/stdc++.h>
using namespace std;
struct TBlock {
  long long l;
  char c;
  TBlock() {}
  TBlock(long long _l, char _c) : l(_l), c(_c) {}
  TBlock(char* s) {
    char* tok = strtok(s, " -\n");
    l = atoll(tok);
    tok = strtok(NULL, " -\n");
    c = tok[0];
  }
};
int n, m;
vector<TBlock> s, t;
vector<int> Z, Zn;
int main() {
  char buff[10];
  scanf("%d %d", &n, &m);
  s.clear();
  t.clear();
  for (int i = 0; i < n; ++i) {
    scanf(" %s", buff);
    TBlock tmp = TBlock(buff);
    if (s.size() > 0 && s[s.size() - 1].c == tmp.c)
      s[s.size() - 1].l += tmp.l;
    else
      s.push_back(tmp);
  }
  for (int i = 0; i < m; ++i) {
    scanf(" %s", buff);
    TBlock tmp = TBlock(buff);
    if (t.size() > 0 && t[t.size() - 1].c == tmp.c)
      t[t.size() - 1].l += tmp.l;
    else
      t.push_back(tmp);
  }
  n = s.size();
  m = t.size();
  if (m == 1) {
    long long res = 0;
    for (int i = 0; i < n; ++i)
      if (s[i].c == t[0].c && s[i].l >= t[0].l) res += s[i].l - t[0].l + 1;
    printf("%I64d\n", res);
    return 0;
  }
  t.insert(t.end(), TBlock(0, '#'));
  t.insert(t.end(), s.begin(), s.end());
  Z.resize(n + m + 1, 0);
  int left = 0, right = 0;
  for (int i = 1; i < n + m + 1; ++i) {
    if (right > i) Z[i] = min(Z[i - left], right - i + 1);
    while (i + Z[i] < n + m + 1 && t[i + Z[i]].c == t[Z[i]].c) ++Z[i];
    if (i + Z[i] - 1 > right) {
      left = i;
      right = i + Z[i] - 1;
    }
  }
  Zn.resize(n + m + 1, 0);
  left = right = 1;
  for (int i = 2; i < n + m + 1; ++i) {
    if (right > i) Zn[i] = min(Zn[i - left + 1], right - i + 1);
    while (i + Zn[i] < n + m + 1 && t[i + Zn[i]].l == t[Zn[i] + 1].l) ++Zn[i];
    if (i + Zn[i] - 1 > right) {
      left = i;
      right = i + Zn[i] - 1;
    }
  }
  int res = 0;
  for (int i = m + 1; i < n + m + 1; ++i) {
    if (Z[i] >= m) {
      if (t[i].l >= t[0].l && t[i + m - 1].l >= t[m - 1].l &&
          Zn[i + 1] >= m - 2) {
        ++res;
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
