#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, c1[233333], c2[233333], ans, x, nxt[233333];
char s1[233333], s2[233333];
int main() {
  cin >> n >> m;
  j = 0;
  for (i = 1; i <= n; i++) {
    j++;
    scanf("%I64d-%c", c1 + j, &s1[j]);
    if (s1[j] == s1[j - 1]) {
      c1[j - 1] += c1[j];
      j--;
    }
  }
  n = j;
  j = -1;
  for (i = 1; i <= m; i++) {
    j++;
    scanf("%I64d-%c", c2 + j, &s2[j]);
    if (s2[j] == s2[j - 1]) {
      c2[j - 1] += c2[j];
      j--;
    }
  }
  m = j;
  if (!m) {
    for (i = 1; i <= n; i++) {
      if (s1[i] == s2[0] && c1[i] >= c2[0]) {
        ans += c1[i] - c2[0] + 1;
      }
    }
    cout << ans;
    return 0;
  }
  for (i = 2; i <= m - 1; i++) {
    while (j > 0 && (s2[j + 1] != s2[i] || c2[j + 1] != c2[i])) j = nxt[j];
    if (s2[j + 1] == s2[i] && c2[j + 1] == c2[i]) j++;
    nxt[i] = j;
  }
  j = 0;
  for (i = 1; i <= n; i++) {
    while (j > 0 && (s2[j + 1] != s1[i] || c2[j + 1] != c1[i])) j = nxt[j];
    if (s2[j + 1] == s1[i] && c2[j + 1] == c1[i]) j++;
    if (j == m - 1) {
      x = i - m + 1;
      if (s1[x] == s2[0] && s1[i + 1] == s2[m] && c1[x] >= c2[0] &&
          c1[i + 1] >= c2[m])
        ans++;
      j = nxt[j];
    }
  }
  cout << ans;
  return 0;
}
