#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%lld", &x); }
void _R(unsigned long long &x) { scanf("%llu", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const long long &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const int MOD = 1e9 + 7, mod = 998244353;
long long qpow(long long a, long long b) {
  long long res = 1;
  a %= MOD;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
const int MAXN = 4e5 + 10, MAXM = 2e6 + 10;
const int INF = INT_MAX, SINF = 0x3f3f3f3f;
const long long llINF = LLONG_MAX;
const int inv2 = (MOD + 1) / 2;
const int Lim = 1 << 20;
int n, m, n1, m1;
char s[MAXN], t[MAXN];
long long cs[MAXN], ct[MAXN], nxt[MAXN];
long long ans = 0;
void kmp() {
  if (m == 1) {
    for (int i = 0; i < n; i++)
      if (s[i] == t[0]) ans += max(0ll, cs[i] - ct[0] + 1);
    return;
  }
  if (m == 2) {
    for (int i = 0; i < n - 1; i++)
      if (s[i] == t[0] && cs[i] >= ct[0] && s[i + 1] == t[1] &&
          cs[i + 1] >= ct[1])
        ans++;
    return;
  }
  nxt[1] = 0;
  for (int i = 2; i < m - 1; i++) {
    int p = nxt[i - 1];
    while ((ct[p + 1] != ct[i] || t[p + 1] != t[i]) && p > 1) p = nxt[p];
    if (ct[p + 1] == ct[i] && t[p + 1] == t[i]) p++;
    nxt[i] = p;
  }
  int u = 0;
  for (int i = 1; i < n - 1; i++) {
    while ((ct[u + 1] != cs[i] || t[u + 1] != s[i]) && u) u = nxt[u];
    if (ct[u + 1] == cs[i] && t[u + 1] == s[i]) u++;
    if (u == m - 2) {
      if ((cs[i - u] >= ct[0] && s[i - u] == t[0]) &&
          (cs[i + 1] >= ct[m - 1] && s[i + 1] == t[m - 1]))
        ans++;
      u = nxt[u];
    }
  }
}
void solve() {
  R(n1, m1);
  for (int i = 1; i <= n1; i++) {
    int tot;
    R(tot);
    char input[5];
    R(input);
    if (n && s[n - 1] == input[1])
      cs[n - 1] += tot;
    else
      s[n] = input[1], cs[n++] = tot;
  }
  for (int i = 1; i <= m1; i++) {
    int tot;
    R(tot);
    char input[5];
    R(input);
    if (m && t[m - 1] == input[1])
      ct[m - 1] += tot;
    else
      t[m] = input[1], ct[m++] = tot;
  }
  kmp();
  W(ans);
}
int main() {
  srand(time(0));
  int T = 1;
  while (T--) solve();
  return 0;
}
