#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 50;
vector<pair<long long, char> > s(maxn), t(maxn), st(maxn);
int len1, len2, cntt;
int z[maxn];
void get_z() {
  int l = 0, r = 0;
  for (int i = 1; i < cntt; i++) {
    if (i > r) {
      l = i, r = i;
      while (r < cntt && st[r - l] == st[r]) r++;
      z[i] = r - l, r--;
    } else {
      int k = i - l;
      if (z[k] < r - i + 1)
        z[i] = z[k];
      else {
        l = i;
        while (r < cntt && st[r - l] == st[r]) r++;
        z[i] = r - l, r--;
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    long long x;
    char y;
    scanf("%lld-%c", &x, &y);
    if (!i)
      s[cnt++] = {x, y};
    else if (y != s[cnt - 1].second)
      s[cnt++] = {x, y};
    else
      s[cnt - 1].first += x;
  }
  len1 = cnt;
  cnt = 0;
  for (int i = 0; i < m; i++) {
    long long x;
    char y;
    scanf("%lld-%c", &x, &y);
    if (!i)
      t[cnt++] = {x, y};
    else if (y != t[cnt - 1].second)
      t[cnt++] = {x, y};
    else
      t[cnt - 1].first += x;
  }
  len2 = cnt;
  if (len2 == 1) {
    long long ans = 0;
    for (int i = 0; i < len1; i++) {
      if (s[i].second == t[0].second) {
        ans += max(0ll, s[i].first - t[0].first + 1ll);
      }
    }
    printf("%lld\n", ans);
    return 0;
  }
  if (len2 == 2) {
    long long ans = 0;
    for (int i = 0; i < len1 - 1; i++) {
      if (s[i].second == t[0].second && s[i + 1].second == t[1].second) {
        if (s[i].first >= t[0].first && s[i + 1].first >= t[1].first) ans++;
      }
    }
    printf("%lld\n", ans);
    return 0;
  }
  cntt = 0;
  for (int i = 1; i < len2 - 1; i++) {
    st[cntt++] = t[i];
  }
  st[cntt++] = {0, 0};
  for (int i = 0; i < len1; i++) {
    st[cntt++] = s[i];
  }
  get_z();
  long long ans = 0;
  for (int i = len2 - 1; i < cntt; i++) {
    if (z[i] >= len2 - 2) {
      if (st[i - 1].second == t[0].second && st[i - 1].first >= t[0].first &&
          st[i + len2 - 2].second == t[len2 - 1].second &&
          st[i + len2 - 2].first >= t[len2 - 1].first) {
        ans++;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
