#include <bits/stdc++.h>
using namespace std;
inline int ReadInt() {
  int n = 0, ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
  return n;
}
inline int ReadChar() {
  int ch = getchar();
  while (!(ch >= 'a' && ch <= 'z')) ch = getchar();
  return ch;
}
const int maxn = 200000 + 3;
long long ca[maxn], la[maxn], cb[maxn], lb[maxn];
int a, b;
int get(int x, long long *c, long long *l) {
  long long n = 0, cnt = ReadInt(), lastc = ReadChar();
  for (int i = 1; i < x; ++i) {
    long long li = ReadInt(), ci = ReadChar();
    if (ci != lastc) {
      c[n] = lastc;
      l[n] = cnt;
      cnt = 0;
      n++;
    }
    lastc = ci, cnt += li;
  }
  c[n] = lastc;
  l[n] = cnt;
  n++;
  return n;
}
int f[maxn];
bool match(int i, int j) {
  if (ca[i] != cb[j]) return false;
  if (j != 0 && j + 1 != b && la[i] != lb[j]) return false;
  if ((j == 0 || j + 1 == b) && la[i] < lb[j]) {
    return false;
  }
  return true;
}
long long kmp() {
  long long cnt = 0;
  int j = 0;
  for (int i = 0; i < a; ++i) {
    while (j && !match(i, j)) j = f[j];
    if (match(i, j)) j++;
    if (j == b) {
      cnt++;
    }
  }
  return cnt;
}
int main() {
  int n, m;
  cin >> n >> m;
  a = get(n, ca, la);
  b = get(m, cb, lb);
  if (b != 1) {
    lb[b] = 0;
    f[0] = f[1] = 0;
    for (int i = 1; i < b; ++i) {
      int j = f[i];
      while (j && cb[i] != cb[j]) j = f[j];
      f[i + 1] = cb[i] == cb[f[i]] ? j + 1 : 0;
    }
    cout << kmp() << endl;
  } else {
    long long cnt = 0;
    for (int i = 0; i < a; ++i)
      if (ca[i] == cb[0] && la[i] >= lb[0]) cnt += la[i] - lb[0] + 1;
    cout << cnt << endl;
  }
  return 0;
}
