#include <bits/stdc++.h>
using namespace std;
const double eps = 0.000000000000001;
long long powd(long long a, long long b, long long c) {
  if (b == 0) return 1;
  long long ret = powd(a, b / 2, c);
  if (b & 1)
    return (((a * ret) % c) * ret) % c;
  else
    return (ret * ret) % c;
}
int *makeprefix(char *s, int n, int *a);
int l1, l2;
char s1[200010], s2[200010];
long long c1[200010], c2[200010];
long long hs[200010] = {0};
long long b1 = 15485867, b2 = 15485867;
const long long M = (1000000007);
int pre[200010];
long long hs1[200010], hs2[200010], inv1[200010], inv2[200010];
pair<long long, long long> calc() {
  int i;
  long long hh1 = 0, hh2 = 0;
  long long mul1 = 1, mul2 = 1;
  for (i = 0; i < l2; i++) {
    hh1 = ((hh1) % 1000000007 + (c2[i] * mul1) % 1000000007) % 1000000007;
    hh2 = ((hh2) % 1000000007 + (c2[i] * mul2) % 1000000007) % 1000000007;
    mul1 = (mul1 * b1) % 1000000007;
    mul2 = (mul2 * b2) % 1000000007;
  }
  long long hok1 = 0, hok2 = 0;
  mul1 = mul2 = 1;
  for (i = 0; i < l1; i++) {
    hok1 = ((hok1) % 1000000007 + (c1[i] * mul1) % 1000000007) % 1000000007;
    hs1[i] = hok1;
    hok2 = ((hok2) % 1000000007 + (c1[i] * mul2) % 1000000007) % 1000000007;
    hs2[i] = hok2;
    mul1 = (mul1 * b1) % 1000000007;
    mul2 = (mul2 * b2) % 1000000007;
  }
  long long iv = powd(b1, 1000000007 - 2, 1000000007);
  inv1[0] = 1;
  for (i = 1; i <= l1; i++) {
    inv1[i] = (inv1[i - 1] * iv) % 1000000007;
  }
  inv2[0] = 1;
  iv = powd(b2, 1000000007 - 2, 1000000007);
  for (i = 1; i <= l1; i++) inv2[i] = (inv2[i - 1] * iv) % 1000000007;
  return make_pair((long long)hh1, (long long)hh2);
}
pair<long long, long long> hagre;
void calchagre() {
  long long h1 = 0, h2 = 0;
  h1 = c2[0] % 1000000007;
  h2 = h1;
  h1 = (h1 + (powd(b1, l2 - 1, 1000000007) * (c2[l2 - 1] % 1000000007)) %
                 1000000007) %
       1000000007;
  h2 = (h2 + (powd(b2, l2 - 1, 1000000007) * (c2[l2 - 1] % 1000000007)) %
                 1000000007) %
       1000000007;
  hagre = make_pair(h1, h2);
}
pair<long long, long long> hato(int x, int y) {
  long long hon1 = 0, hon2 = 0;
  x++;
  y--;
  if (x <= y) {
    hon1 = hs1[y] - hs1[x - 1];
    hon1 = (hon1 * inv1[x - 1]) % 1000000007;
    hon2 = hs2[y] - hs2[x - 1];
    hon2 = (hon2 * inv2[x - 1]) % 1000000007;
  }
  hon1 = (hon1 + hagre.first) % 1000000007;
  hon2 = (hon2 + hagre.second) % 1000000007;
  hon1 = (hon1 % 1000000007 + 1000000007) % 1000000007;
  hon2 = (hon2 % 1000000007 + 1000000007) % 1000000007;
  return make_pair(hon1, hon2);
}
void logic(char *s1, char *s2) {
  makeprefix(s2, l2, pre);
  int i, j;
  pair<long long, long long> his = calc();
  calchagre();
  int k = 0;
  long long ans = 0;
  for (i = 1; i <= l1; i++) {
    while (k > 0 && s2[k + 1] != s1[i]) k = pre[k];
    if (s2[k + 1] == s1[i]) k++;
    if (k == l2) {
      if (c1[i - l2] >= c2[0] && c1[i - 1] >= c2[l2 - 1]) {
        pair<long long, long long> yo = hato(i - l2, i - 1);
        if (yo == his) {
          ans++;
        }
      }
      k = pre[k];
    }
  }
  printf("%lld\n", ans);
  exit(0);
}
void yo() {
  long long ans = 0;
  int i;
  for (i = 0; i < l1; i++) {
    if (s1[i] == s2[0]) {
      if (c1[i] >= c2[0]) {
        ans += c1[i] - c2[0] + 1;
      }
    }
  }
  printf("%lld\n", ans);
  exit(0);
}
int *makeprefix(char *s, int n, int *a) {
  a[1] = 0;
  int i, k = 0;
  for (i = 2; i <= n; i++) {
    while (k > 0 && s[k + 1] != s[i]) k = a[k];
    if (s[k + 1] == s[i]) k++;
    a[i] = k;
  }
  return a;
}
int main() {
  scanf("%d%d", &l1, &l2);
  int i;
  char lst = -1;
  int p = 0;
  for (i = 0; i < l1; i++) {
    int x;
    char xx[10];
    scanf("%d-%s", &x, xx);
    if (xx[0] == lst) {
      c1[p - 1] += x;
    } else {
      lst = xx[0];
      c1[p] = x;
      s1[p] = xx[0];
      p++;
    }
  }
  l1 = p;
  p = 0;
  lst = -1;
  for (i = 0; i < l2; i++) {
    int x;
    char xx[10];
    scanf("%d-%s", &x, xx);
    if (xx[0] == lst) {
      c2[p - 1] += x;
    } else {
      lst = xx[0];
      c2[p] = x;
      s2[p] = xx[0];
      p++;
    }
  }
  l2 = p;
  if (l2 == 1) {
    yo();
  }
  if (l2 > l1) {
    printf("0\n");
    return 0;
  }
  logic(s1 - 1, s2 - 1);
  return 0;
}
