#include <bits/stdc++.h>
using namespace std;
const long long mod1 = 1000000007;
const long long mod2 = 1000000009;
struct dat {
  long long x, y;
  dat(long long x = 0, long long y = 0) : x(x), y(y) {}
};
vector<dat> v, w;
long long n, m;
long long col[1000001], f[1000001], g[1000001];
long long sum[1000001];
long long mul(long long x, long long y, long long z, long long mod) {
  long long X = 1ll * x * y % mod;
  return 1ll * X * z % mod;
}
long long PW(long long a, long long b, long long c) {
  if (!b) return 1;
  long long d = PW(a, b / 2, c);
  d = 1ll * d * d % c;
  if (b & 1) return 1ll * d * a % c;
  return d;
}
long long PO1(long long x) {
  long long y = PW(26, x + 1, mod1) - 1;
  long long z = PW(25, mod1 - 2, mod1);
  return 1ll * y * z % mod1;
}
long long PO2(long long x) {
  long long y = PW(26, x + 1, mod2) - 1;
  long long z = PW(25, mod2 - 2, mod2);
  return 1ll * y * z % mod2;
}
long long calc1(long long P) {
  long long x = upper_bound(sum + 1, sum + v.size() + 1, P) - sum;
  x--;
  long long y;
  if (P != sum[x])
    y = mul(1, PO1(P - sum[x] - 1), col[x + 1], mod1);
  else
    y = 0;
  y = 1ll * y * PW(26, sum[x], mod1) % mod1;
  return (f[x] + y) % mod1;
}
long long calc2(long long P) {
  long long x = upper_bound(sum + 1, sum + v.size() + 1, P) - sum;
  x--;
  long long y;
  if (P != sum[x])
    y = mul(1, PO2(P - sum[x] - 1), col[x + 1], mod2);
  else
    y = 0;
  y = 1ll * y * PW(26, sum[x], mod2) % mod2;
  return (f[x] + y) % mod2;
}
int main() {
  scanf("%I64d %I64d", &n, &m);
  for (long long i = 1; i <= n; i++) {
    long long x;
    char s[10];
    scanf("%I64d-%s\n", &x, s);
    long long sz = v.size();
    if (sz && v[sz - 1].y == s[0] - 'a')
      v[sz - 1].x += x;
    else {
      v.push_back(dat(x, s[0] - 'a'));
    }
  }
  for (long long i = 1; i <= m; i++) {
    long long x;
    char s[10];
    scanf("%I64d-%s\n", &x, s);
    long long sz = w.size();
    if (sz && w[sz - 1].y == s[0] - 'a')
      w[sz - 1].x += x;
    else {
      w.push_back(dat(x, s[0] - 'a'));
    }
  }
  if (w.size() == 1) {
    long long ans = 0;
    for (long long i = 0; i < v.size(); i++) {
      long long x = v[i].x, y = v[i].y;
      if (y != w[0].y || x < w[0].x) continue;
      ans += x - w[0].x + 1;
    }
    printf("%I64d\n", ans);
    return 0;
  }
  long long X1 = 0, X2 = 0, sz = w.size();
  long long LEN = 0;
  for (long long i = 0; i < sz - 1; i++) {
    X1 = (X1 + mul(w[i].y, PO1(w[i].x - 1), PW(26, LEN, mod1), mod1)) % mod1;
    X2 = (X2 + mul(w[i].y, PO2(w[i].x - 1), PW(26, LEN, mod2), mod2)) % mod2;
    LEN += w[i].x;
  }
  for (long long i = 1; i <= v.size(); i++) {
    sum[i] = sum[i - 1] + v[i - 1].x;
    col[i] = v[i - 1].y;
  }
  for (long long i = 1; i <= v.size(); i++) {
    f[i] = (f[i - 1] + mul(v[i - 1].y, PO1(v[i - 1].x - 1),
                           PW(26, sum[i - 1], mod1), mod1)) %
           mod1;
    g[i] = (g[i - 1] + mul(v[i - 1].y, PO2(v[i - 1].x - 1),
                           PW(26, sum[i - 1], mod2), mod2)) %
           mod2;
  }
  long long ans = 0;
  for (long long i = 1; i <= v.size(); i++) {
    if (col[i] == w[sz - 1].y && v[i - 1].x >= w[sz - 1].x &&
        sum[i - 1] >= LEN) {
      long long P = sum[i - 1] - LEN;
      long long x = calc1(P);
      long long y = f[i - 1] - x;
      if (y < 0) y += mod1;
      long long z = mul(1, PW(26, P, mod1), X1, mod1);
      if (z != y) continue;
      ans++;
    }
  }
  printf("%I64d\n", ans);
}
