#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200100;
int n, m;
pair<long long, char> A[MAXN], B[MAXN];
void load() {
  scanf("%d%d", &n, &m);
  int x;
  char c;
  int n_ = 0, m_ = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d -%c", &x, &c);
    if (n_ && c == A[n_ - 1].second)
      A[n_ - 1].first += (long long)x;
    else
      A[n_++] = pair<long long, char>(x, c);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d -%c", &x, &c);
    if (m_ && c == B[m_ - 1].second)
      B[m_ - 1].first += (long long)x;
    else
      B[m_++] = pair<long long, char>(x, c);
  }
  n = n_;
  m = m_;
}
pair<long long, char> s[MAXN * 2];
int z[MAXN * 2];
void solve() {
  int len = 0;
  for (int i = 1; i < m - 1; i++) {
    s[len++] = B[i];
  }
  s[len++] = pair<int, int>(-1, -1);
  for (int i = 0; i < n; i++) s[len++] = A[i];
  int l = 0, r = 0;
  for (int i = 0; i < len; i++) z[i] = 0;
  z[0] = len;
  for (int i = 1; i < len; i++) {
    if (i > r) {
      l = r = i;
      while (r < len && s[r] == s[r - l]) 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 < len && s[r - l] == s[r]) r++;
        z[i] = r - l;
        r--;
      }
    }
  }
  long long ret = 0;
  for (int i = m - 1; i < len; i++) {
    int j = i - m + 1;
    int k = j + m - 3;
    if (z[i] == m - 2) {
      if (j && k < n - 1) {
        if (A[j - 1].second == B[0].second &&
            A[k + 1].second == B[m - 1].second &&
            A[j - 1].first >= B[0].first && A[k + 1].first >= B[m - 1].first)
          ret++;
      }
    }
  }
  printf("%I64d\n", ret);
}
void solve2() {
  long long ret = 0;
  for (int i = 0; i < n - 1; i++) {
    if (A[i].second == B[0].second && A[i + 1].second == B[1].second) {
      if (A[i].first >= B[0].first && A[i + 1].first >= B[1].first) ret++;
    }
  }
  printf("%I64d\n", ret);
}
void solve1() {
  char c = B[0].second;
  long long len = B[0].first;
  long long ret = 0;
  for (int i = 0; i < n; i++) {
    if (c == A[i].second && A[i].first >= len) {
      ret += A[i].first - len + 1;
    }
  }
  printf("%I64d\n", ret);
}
int main() {
  load();
  if (m == 1)
    solve1();
  else if (m == 2)
    solve2();
  else
    solve();
}
