#include <bits/stdc++.h>
using namespace std;
long long na[200010];
char a[200010];
long long nb[200010];
char b[200010];
long long f[200010];
int n, m;
bool check(int l, int r) {
  if (a[l - 1] == b[0] && a[r + 1] == b[m - 1] && na[l - 1] >= nb[0] &&
      na[r + 1] >= nb[m - 1])
    return true;
  return false;
}
void kmp(char *a, char *b, int n, int m, long long *na, long long *nb,
         long long &ans) {
  f[0] = -1;
  for (int i = 1; i < m; i++) {
    int j = f[i - 1];
    while ((b[j + 1] != b[i] || nb[j + 1] != nb[i]) && (j >= 0)) j = f[j];
    if (b[j + 1] == b[i] && nb[j + 1] == nb[i])
      f[i] = j + 1;
    else
      f[i] = -1;
  }
  int i = 0, j = 0;
  while (i < n) {
    if (a[i] == b[j] && na[i] == nb[j]) {
      i++;
      j++;
      if (j == m) {
        if (check(i - m + 1, i)) ans++;
        j = f[j - 1] + 1;
      }
    } else {
      if (j == 0)
        i++;
      else
        j = f[j - 1] + 1;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%I64d-%c", &na[i], &a[i]);
    if (i >= 1 && a[i] == a[i - 1]) {
      na[i - 1] += na[i];
      i--;
      n--;
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%I64d-%c", &nb[i], &b[i]);
    if (i >= 1 && b[i] == b[i - 1]) {
      nb[i - 1] += nb[i];
      i--;
      m--;
    }
  }
  long long ans = 0;
  if (m == 1) {
    for (int i = 0; i < n; i++) {
      if (a[i] == b[0] && na[i] >= nb[0]) {
        ans += (long long)na[i] - nb[0] + 1;
      }
    }
  } else if (m == 2) {
    for (int i = 1; i < n; i++) {
      if (a[i - 1] == b[0] && a[i] == b[1] && na[i - 1] >= nb[0] &&
          na[i] >= nb[1]) {
        ans++;
      }
    }
  } else {
    kmp(a + 1, b + 1, n - 2, m - 2, na + 1, nb + 1, ans);
  }
  cout << ans << endl;
}
