#include <bits/stdc++.h>
using namespace std;
const int kN = 2e5 + 10;
int N, M;
struct Ele {
  long long len;
  char c;
  void read() {
    len = 0;
    char s[30];
    scanf("%s", s);
    for (int i = 0; s[i]; i++) {
      if (s[i] == '-') {
        c = s[i + 1];
        break;
      } else {
        len *= 10;
        len += s[i] - '0';
      }
    }
  }
  bool operator==(const Ele &e) const { return len == e.len && c == e.c; }
  bool operator!=(const Ele &e) const { return !((*this) == e); }
};
Ele A[kN], B[kN], T[kN];
int tn;
void merge(Ele a[], int &n) {
  int p = 0;
  for (int i = 1; i <= n; i++) {
    if (p && a[p].c == a[i].c) {
      a[p].len += a[i].len;
    } else {
      p++;
      a[p] = a[i];
    }
  }
  n = p;
}
bool check(int i, int j) {
  return i >= 1 && i <= N && A[i].len >= B[j].len && A[i].c == B[j].c;
}
int lst[kN];
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++) A[i].read();
  for (int i = 1; i <= M; i++) B[i].read();
  merge(A, N);
  merge(B, M);
  for (int i = 2; i < M; i++) T[++tn] = B[i];
  long long ans = 0;
  if (M == 1) {
    for (int i = 1; i <= N; i++) {
      if (A[i].c == B[1].c && A[i].len >= B[1].len) {
        ans += A[i].len - B[1].len + 1;
      }
    }
  } else {
    lst[1] = 0;
    for (int i = 2; i <= tn; i++) {
      int p = lst[i - 1];
      while (p && T[p + 1] != T[i]) p = lst[p];
      if (T[p + 1] == T[i]) p++;
      lst[i] = p;
    }
    int j = 0;
    for (int i = 1; i <= N; i++) {
      while (j < tn && j && T[j + 1] != A[i]) j = lst[j];
      if (tn && T[j + 1] == A[i]) j++;
      if (j == tn) {
        if (check(i + 1, M) && check(i - tn, 1)) {
          ans++;
        }
        j = lst[j];
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
