#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T, class U>
inline void Max(T &a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
inline void Min(T &a, U b) {
  if (a > b) a = b;
}
inline void add(int &a, int b) {
  a += b;
  while (a >= 1000000007) a -= 1000000007;
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 1000000007;
    a = 1LL * a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
const int q = 37, M = 1000000007;
pair<long long, int> a[200010], b[200010];
void cal(pair<long long, int> a[], int &n) {
  int m = 0;
  for (int i = 0; i < n; i++) {
    int j = i + 1;
    long long first = a[i].first;
    while (j < n && a[j].second == a[i].second) first += a[j++].first;
    a[m++] = make_pair(first, a[i].second);
    i = j - 1;
  }
  n = m;
}
long long qq[200010], h[200010], g[200010], e[200010];
int main() {
  int i, j, k, T, n, m;
  scanf("%d%d", &n, &m);
  char c;
  for (i = 0; i < n; i++) {
    scanf("%d-%c", &a[i].first, &c);
    a[i].second = c;
  }
  for (i = 0; i < m; i++) {
    scanf("%d-%c", &b[i].first, &c);
    b[i].second = c;
  }
  cal(a, n);
  cal(b, m);
  if (n < m) {
    puts("0");
    return 0;
  }
  if (m == 1) {
    long long ans = 0;
    for (i = 0; i < n; i++)
      if (a[i].first >= b[0].first && a[i].second == b[0].second)
        ans += a[i].first - b[0].first + 1;
    cout << ans << "\n";
    return 0;
  }
  if (m == 2) {
    int ans = 0;
    for (i = 0; i < n - 1; i++)
      if (a[i].first >= b[0].first && a[i].second == b[0].second &&
          a[i + 1].first >= b[1].first && a[i + 1].second == b[1].second)
        ans++;
    cout << ans << "\n";
    return 0;
  }
  qq[0] = 1;
  for (i = 0; i < n; i++) qq[i + 1] = qq[i] * q % M;
  long long s1 = 0, s2 = 0, s3 = 0;
  for (i = 1; i < m - 1; i++) {
    int t = b[i].first % M + 1;
    s1 = (s1 * q + t) % M;
    t = b[i].first / M + 1;
    s2 = (s2 * q + t) % M;
    s3 = (s3 * q + b[i].second) % M;
  }
  for (i = 0; i < n; i++) {
    int t = a[i].first % M + 1;
    h[i + 1] = (h[i] * q + t) % M;
    t = a[i].first / M + 1;
    g[i + 1] = (g[i] * q + t) % M;
    e[i + 1] = (e[i] * q + a[i].second) % M;
  }
  int ans = 0;
  for (i = 0; i <= n - m; i++)
    if (a[i].second == b[0].second && a[i + m - 1].second == b[m - 1].second &&
        a[i].first >= b[0].first && a[i + m - 1].first >= b[m - 1].first) {
      int t = (h[i + m - 1] - h[i + 1] * qq[m - 2] % M + M) % M;
      if (t != s1) continue;
      t = (g[i + m - 1] - g[i + 1] * qq[m - 2] % M + M) % M;
      if (t != s2) continue;
      t = (e[i + m - 1] - e[i + 1] * qq[m - 2] % M + M) % M;
      if (t == s3) ans++;
    }
  printf("%d\n", ans);
  return 0;
}
