#include <bits/stdc++.h>
using namespace std;
char a[200086], b[200086];
long long la, lb, ext[200086], sa[200086], sb[200086];
int getnext(int x) {
  int i, j = 0;
  for (i = 1; i <= x;) {
    if (j == 0 || b[i] == b[j]) {
      i++;
      j++;
      ext[i] = j;
    } else {
      j = ext[j];
    }
  }
}
int main() {
  int i, j;
  while (~scanf("%d%d", &la, &lb)) {
    int ppap = la;
    int pdd = lb;
    for (i = 1; i <= la; i++) {
      scanf("%d-%c", &sa[i], &a[i]);
      if (a[i] == a[i - 1]) {
        sa[i - 1] += sa[i];
        i--;
        la--;
      }
    }
    for (i = 1; i <= lb; i++) {
      scanf("%d-%c", &sb[i], &b[i]);
      if (b[i] == b[i - 1]) {
        sb[i - 1] += sb[i];
        i--;
        lb--;
      }
    }
    long long ans = 0;
    if (lb == 1) {
      for (i = 1; i <= la; i++) {
        if (a[i] == b[1] && sa[i] >= sb[1]) {
          ans += sa[i] - sb[1] + 1;
        }
      }
      cout << ans << endl;
      continue;
    }
    if (lb == 2) {
      for (i = 2; i <= la; i++) {
        if (b[1] == a[i - 1] && sb[1] <= sa[i - 1] && b[2] == a[i] &&
            sb[2] <= sa[i]) {
          ans++;
        }
      }
      cout << ans << endl;
      continue;
    }
    getnext(lb);
    int flag = 1;
    for (i = 1, j = 1; i <= la && j <= lb;) {
      if (a[i] == b[j + 1] && sa[i] == sb[j + 1]) {
        i++;
        j++;
      } else {
        if (ext[j] == 0) {
          j = 1;
          i++;
        } else {
          j = ext[j];
        }
      }
      if (j == lb - 1) {
        if (sb[1] <= sa[i - j] && sb[j + 1] <= sa[i] && b[1] == a[i - j] &&
            b[j + 1] == a[i]) {
          ans++;
        }
        j = ext[j];
      }
    }
    cout << ans << endl;
  }
}
