#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 200005;
int nex[maxn];
struct no {
  char c;
  long long v;
  bool operator==(const no &b) const { return (c == b.c) && (v == b.v); }
  bool operator<=(const no &b) const { return (c == b.c) && (v <= b.v); }
};
long long n, m;
no a[maxn], b[maxn];
void compress(no *a, long long &x) {
  long long n = 1;
  int i;
  for (i = 2; i <= x; i++) {
    if (a[n].c == a[i].c) {
      a[n].v += a[i].v;
    } else {
      n++;
      a[n].v = a[i].v;
      a[n].c = a[i].c;
    }
  }
  x = n;
}
void solve() {
  int i, j, k;
  scanf("%I64d%I64d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%I64d-%c", &a[i].v, &a[i].c);
  }
  compress(a, n);
  for (i = 1; i <= m; i++) {
    scanf("%I64d-%c", &b[i].v, &b[i].c);
  }
  compress(b, m);
  long long res = 0;
  if (m == 1) {
    for (i = 1; i <= n; i++) {
      if (b[1] <= a[i]) {
        res += (a[i].v - b[1].v + 1);
      }
    }
  } else if (m == 2) {
    for (i = 2; i <= n; i++) {
      if (b[1] <= a[i - 1] && b[2] <= a[i]) {
        res++;
      }
    }
  } else {
    j = 0;
    for (i = 1; i <= m;) {
      if (b[i] == b[j] || j == 0) {
        i++;
        j++;
        nex[i] = j;
      } else {
        j = nex[j];
      }
    }
    for (i = 1, j = 1; i <= n && j <= m;) {
      if (b[j + 1] == a[i]) {
        i++;
        j++;
      } else {
        if (nex[j] == 0) {
          j = 1;
          i++;
        } else {
          j = nex[j];
        }
      }
      if (j == m - 1) {
        if (b[1] <= a[i - j] && b[j + 1] <= a[i]) {
          res++;
        }
        j = nex[j];
      }
    }
  }
  printf("%I64d", res);
}
int main() {
  solve();
  return 0;
}
