#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
long long tn[maxn], sn[maxn];
int tc[maxn], sc[maxn], f[maxn];
int n, m, len1, len2;
void read(long long *an, int *ac, int &len3, int n) {
  char ch[100];
  long long amt = 0;
  len3 = 0;
  int c = -1;
  for (int i = 0; i < n; i++) {
    scanf("%s", ch);
    long long sum = 0;
    int len = strlen(ch), j;
    for (j = 0; ch[j] != '-'; j++) sum = 10 * sum + ch[j] - '0';
    if (c == -1) {
      c = ch[j + 1];
      amt = sum;
    } else if (ch[j + 1] == c)
      amt += sum;
    else {
      ac[len3] = c;
      an[len3++] = amt;
      c = ch[j + 1];
      amt = sum;
    }
  }
  ac[len3] = c;
  an[len3++] = amt;
}
void getFail() {
  memset(f, 0, sizeof(f));
  for (int i = 1; i < len2; i++) {
    int j = f[i];
    while (j) {
      if (sn[j] == sn[i] && sc[j] == sc[i]) break;
      j = f[j];
    }
    if (j)
      f[i + 1] = (sc[i] == sc[j] && sn[i] == sn[j]) ? j + 1 : 0;
    else {
      f[i + 1] = (sc[i] == sc[j] && sn[i] >= sn[j]) ? j + 1 : 0;
    }
  }
}
int find() {
  getFail();
  int j = 0, ans = 0;
  for (int i = 0; i < len1; i++) {
    while (j) {
      if (j > 1 && (sn[j - 1] != tn[i - 1] || sc[j - 1] != tc[i - 1])) {
        j = f[j];
        continue;
      }
      if (sn[j] == tn[i] && sc[j] == tc[i]) break;
      if (j == len2 - 1) {
        if (sn[j] <= tn[i] && sc[j] == tc[i]) break;
      }
      j = f[j];
    }
    if (j == 0) {
      if (sn[j] <= tn[i] && sc[j] == tc[i]) j++;
    } else if (j == len2 - 1) {
      if (sn[j] <= tn[i] && sc[j] == tc[i]) {
        j++;
      }
    } else {
      if (sn[j] == tn[i] && sc[j] == tc[i]) j++;
    }
    if (j == len2) {
      ans++;
      j = f[j];
    }
  }
  return ans;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    read(tn, tc, len1, n);
    read(sn, sc, len2, m);
    if (len2 == 1) {
      long long ans = 0;
      for (int i = 0; i < len1; i++)
        if (tn[i] >= sn[0] && tc[i] == sc[0])
          ans += (long long)(tn[i] - sn[0] + 1);
      printf("%I64d\n", ans);
    } else if (len2 == 2) {
      int ans = 0;
      for (int i = 0; i < len1 - 1; i++) {
        bool ok1 = tn[i] >= sn[0] && tc[i] == sc[0];
        bool ok2 = tn[i + 1] >= sn[1] && tc[i + 1] == sc[1];
        if (ok1 && ok2) ans++;
      }
      printf("%d\n", ans);
    } else
      printf("%d\n", find());
  }
  return 0;
}
