#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
template <class T>
T gcd(T a, T b) {
  return a ? gcd(b % a, a) : b;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T sgn(T a) {
  return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
int n;
int m;
pair<long long, char> a[500100], b[200100], c[200100];
int p[500100];
int main() {
  scanf("%d%d", &n, &m);
  int cn = 1;
  for (int i = 0; i < n; i++) {
    scanf(" %I64d-%c", &c[cn].first, &c[cn].second);
    if (c[cn - 1].second == c[cn].second)
      c[cn - 1].first += c[cn].first;
    else
      cn++;
  }
  int cm = 1;
  for (int i = 0; i < m; i++) {
    scanf(" %I64d-%c", &b[cm].first, &b[cm].second);
    if (b[cm - 1].second == b[cm].second)
      b[cm - 1].first += b[cm].first;
    else
      cm++;
  }
  for (int i = 1; i < cn; i++) a[cm + i] = c[i];
  for (int i = 1; i < cm; i++) a[i] = b[i];
  swap(cn, cm);
  cm += cn;
  swap(a[0], a[1]);
  swap(a[cn], a[cn - 1]);
  if (cn == 2) {
    long long ans = 0;
    for (int i = cn + 1; i < cm; i++) {
      if (a[i].second == a[0].second)
        ans += max(0ll, a[i].first - a[0].first + 1);
    }
    cout << ans << endl;
    return 0;
  }
  if (cn == 3) {
    long long ans = 0;
    for (int i = cn + 2; i < cm; i++)
      if (a[i - 1].second == a[0].second && a[i].second == a[cn].second &&
          a[i].first >= a[cn].first && a[i - 1].first >= a[0].first)
        ans++;
    cout << ans << endl;
    return 0;
  }
  p[0] = -1;
  p[2] = 0;
  for (int i = 3; i < cn - 1; i++) {
    int pos = i - 2;
    while (pos > 0 && a[2 + p[pos + 2 - 1]] != a[i]) pos = p[pos + 2 - 1];
    if (pos <= 0)
      p[i] = 0;
    else
      p[i] = p[pos + 2 - 1] + 1;
  }
  p[cn] = 0;
  int ans = 0;
  for (int i = cn + 1; i < cm; i++) {
    int pos = i - 2;
    while (pos > 0 && a[2 + p[pos + 2 - 1]] != a[i]) pos = p[pos + 2 - 1];
    if (pos <= 0)
      p[i] = 0;
    else
      p[i] = p[pos + 2 - 1] + 1;
    if (p[i] == cn - 3) {
      if (i - p[i] >= cn + 1 && a[0].second == a[i - p[i]].second &&
          a[0].first <= a[i - p[i]].first) {
        if (i + 1 < cm && a[cn].second == a[i + 1].second &&
            a[cn].first <= a[i + 1].first)
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
