#include <bits/stdc++.h>
using namespace std;
struct Block {
  char ch;
  long long len;
  Block() { ch = len = 0; }
  Block(char a, int b) {
    ch = a;
    len = b;
  }
  bool operator==(Block &b) { return ch == b.ch && len == b.len; }
  bool operator!=(Block &b) { return !(*this == b); }
};
struct String {
  int sz;
  Block arr[200010];
  String() { sz = 0; }
  void read(int k) {
    int len;
    char ch;
    for (int i(0); i < k; ++i) {
      scanf("%i-%c", &len, &ch);
      if (sz && ch == arr[sz - 1].ch)
        arr[sz - 1].len += (long long)len;
      else
        arr[sz++] = Block(ch, len);
    }
  }
};
bool good(Block &a, Block &b) { return a.ch == b.ch && a.len >= b.len; }
int main() {
  int n, m;
  scanf("%i%i", &n, &m);
  String s, t;
  t.read(n);
  s.read(m);
  long long ans(0);
  if (s.sz == 1) {
    for (int i(0); i < t.sz; ++i)
      if (good(t.arr[i], s.arr[0])) ans += t.arr[i].len - s.arr[0].len + 1;
    printf("%I64d", ans);
    return 0;
  }
  if (s.sz == 2) {
    for (int i(0); i < t.sz - 1; ++i)
      if (good(t.arr[i], s.arr[0]) && good(t.arr[i + 1], s.arr[1])) ans++;
    printf("%I64d", ans);
    return 0;
  }
  if (t.sz <= 2) {
    printf("0");
    return 0;
  }
  int ps[200010], pt[200010];
  ps[0] = 0;
  int r(0), l(0), j;
  for (int i(1); i < s.sz - 2; ++i) {
    j = ps[i - 1];
    while (j && s.arr[1 + j] != s.arr[1 + i]) j = ps[j - 1];
    if (s.arr[j + 1] == s.arr[i + 1])
      ps[i] = j + 1;
    else
      ps[i] = 0;
  }
  r = -1;
  l = 0;
  Block tmp = s.arr[s.sz - 1];
  s.arr[s.sz - 1] = Block(0, 0);
  pt[0] == (s.arr[1] == t.arr[1] ? 1 : 0);
  for (int i(0); i < t.sz - 2; ++i) {
    j = pt[i - 1];
    while (j && s.arr[j + 1] != t.arr[i + 1]) j = ps[j - 1];
    if (s.arr[j + 1] == t.arr[i + 1])
      pt[i] = j + 1;
    else
      pt[i] = 0;
  }
  s.arr[s.sz - 1] = tmp;
  for (int i(0); i < t.sz - 2; ++i)
    if (pt[i] == s.sz - 2)
      if (good(t.arr[i + 3 - s.sz], s.arr[0]) &&
          good(t.arr[i + 2], s.arr[s.sz - 1]))
        ans++;
  printf("%I64d", ans);
}
