#include <bits/stdc++.h>
using namespace std;
const int MXN = (int)1e6 + 10;
const int INF = (int)1e9 + 7;
const long long LINF = (long long)1e18 + 10;
const double EPS = (double)1e-9;
const double PI = (double)acos(-1.0);
int n, m;
long long l1[MXN], l2[MXN], l3[MXN];
char c1[MXN], c2[MXN], c3[MXN];
int p[MXN];
long long l4[MXN], l5[MXN];
char c4[MXN], c5[MXN];
int main() {
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < n; ++i) {
    assert(scanf("%d-%c ", &l1[i], &c1[i]) == 2);
  }
  for (int i = 0; i < m; ++i) {
    assert(scanf("%d-%c ", &l2[i], &c2[i]) == 2);
  }
  int sz = 1;
  l4[0] = l1[0];
  c4[0] = c1[0];
  for (int i = 1; i < n; ++i) {
    if (c1[i] == c4[sz - 1])
      l4[sz - 1] += l1[i];
    else {
      l4[sz] = l1[i];
      c4[sz++] = c1[i];
    }
  }
  for (int i = 0; i < sz; ++i) {
    l1[i] = l4[i];
    c1[i] = c4[i];
  }
  n = sz;
  sz = 1;
  l4[0] = l2[0];
  c4[0] = c2[0];
  for (int i = 1; i < m; ++i) {
    if (c2[i] == c4[sz - 1])
      l4[sz - 1] += l2[i];
    else {
      l4[sz] = l2[i];
      c4[sz++] = c2[i];
    }
  }
  for (int i = 0; i < sz; ++i) {
    l2[i] = l4[i];
    c2[i] = c4[i];
  }
  m = sz;
  if (m == 1) {
    long long ans = 0LL;
    for (int i = 0; i < n; ++i) {
      if (c1[i] == c2[0]) {
        ans += 1LL * max(l1[i] - l2[0] + 1, 0ll);
      }
    }
    printf("%I64d", ans);
  } else if (m == 2) {
    int ans = 0;
    for (int i = 0; i + 1 < n; ++i) {
      if (c1[i] == c2[0] && c1[i + 1] == c2[1] && l1[i] >= l2[0] &&
          l1[i + 1] >= l2[1]) {
        ++ans;
      }
    }
    printf("%d", ans);
  } else {
    int sz = 0;
    int ans = 0;
    for (int i = 1; i < m - 1; ++i) {
      l3[sz] = l2[i];
      c3[sz++] = c2[i];
    }
    l3[sz] = -1;
    c3[sz++] = '#';
    for (int i = 0; i < n; ++i) {
      l3[sz] = l1[i];
      c3[sz++] = c1[i];
    }
    for (int i = 1; i < sz; ++i) {
      int j = p[i - 1];
      while (j > 0 && !(l3[i] == l3[j] && c3[i] == c3[j])) j = p[j - 1];
      if (l3[i] == l3[j] && c3[i] == c3[j]) ++j;
      p[i] = j;
    }
    for (int i = m; i < sz; ++i) {
      if (p[i] == m - 2 && i + 1 < sz &&
          (l3[i + 1] >= l2[m - 1] && c3[i + 1] == c2[m - 1]) &&
          (l3[i - p[i]] >= l2[0] && c3[i - p[i]] == c2[0])) {
        ++ans;
      }
    }
    printf("%d", ans);
  }
  return 0;
}
