#include <bits/stdc++.h>
using namespace std;
struct subs {
  long long x;
  char ch;
  friend bool operator==(subs &a, subs &b) {
    if (a.x == b.x && a.ch == b.ch) return 1;
    return 0;
  }
  friend bool operator!=(subs &a, subs &b) {
    if (a.x != b.x || a.ch != b.ch) return 1;
    return 0;
  }
} t[200200], s[200200], fro, beh;
long long n, m;
int next1[200200];
long long ans;
void makeNext() {
  next1[0] = 0;
  for (int q = 1, k = 0; q < m; ++q) {
    while (k > 0 && s[q] != s[k]) k = next1[k - 1];
    if (s[q] == s[k]) {
      k++;
    }
    next1[q] = k;
  }
}
int kmp() {
  for (int i = 1, q = 0; i < n - 1; ++i) {
    while (q > 0 && s[q] != t[i]) q = next1[q - 1];
    if (s[q] == t[i]) {
      q++;
    }
    if (q == m) {
      if (t[i - m].ch == fro.ch && t[i - m].x >= fro.x &&
          t[i + 1].ch == beh.ch && t[i + 1].x >= beh.x)
        ans++;
    }
  }
}
int main() {
  int i, j;
  while (cin >> n >> m) {
    for (i = 0; i < n; i++) scanf(" %d-%c", &t[i].x, &t[i].ch);
    for (i = 0; i < m; i++) scanf(" %d-%c", &s[i].x, &s[i].ch);
    for (i = 0, j = 1; j < n; j++) {
      if (t[i].ch == t[j].ch)
        t[i].x += t[j].x;
      else {
        i++;
        t[i] = t[j];
      }
    }
    n = i + 1;
    for (i = 0, j = 1; j < m; j++) {
      if (s[i].ch == s[j].ch)
        s[i].x += s[j].x;
      else {
        i++;
        s[i] = s[j];
      }
    }
    m = i + 1;
    ans = 0;
    if (m == 1) {
      for (i = 0; i < n; i++) {
        if (t[i].ch == s[0].ch) ans += max((long long)0, t[i].x - s[0].x + 1);
      }
      cout << ans << endl;
    } else if (m == 2) {
      for (i = 0; i < n - 1; i++) {
        if (t[i].ch == s[0].ch && t[i].x >= s[0].x && t[i + 1].ch == s[1].ch &&
            t[i + 1].x >= s[1].x)
          ans++;
      }
      cout << ans << endl;
    } else {
      fro = s[0];
      beh = s[m - 1];
      m -= 2;
      for (i = 0; i < m; i++) s[i] = s[i + 1];
      makeNext();
      kmp();
      cout << ans << endl;
    }
  }
  return 0;
}
