#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 100;
const int base[] = {131, 131};
const int mod[] = {1000000007, 1000000007};
struct HASH {
  vector<long long> node;
  int hash[2][N], p[2][N];
  ;
  void init_hash() {
    for (int k = 0; k < 2; k++) {
      hash[k][0] = 0;
      for (int i = 1; i < (int)node.size(); i++) {
        hash[k][i] =
            (1LL * hash[k][i - 1] * base[k] + 1LL * node[i] % mod[k]) % mod[k];
      }
    }
  }
  pair<int, int> get_hash(int l, int r) {
    return make_pair(
        (hash[0][r] - 1LL * hash[0][l - 1] * p[0][r - l + 1] % mod[0] +
         mod[0]) %
            mod[0],
        (hash[1][r] - 1LL * hash[1][l - 1] * p[1][r - l + 1] % mod[1] +
         mod[1]) %
            mod[1]);
  }
  void init() {
    for (int k = 0; k < 2; k++) {
      p[k][0] = 1;
      for (int i = 1; i < N; i++)
        p[k][i] = 1LL * p[k][i - 1] * base[k] % mod[k];
    }
  }
} s, t, sn, tn;
int main() {
  s.init(), t.init();
  sn.init(), tn.init();
  int n, m;
  scanf("%d%d", &n, &m);
  s.node.push_back({}), t.node.push_back({});
  sn.node.push_back({}), tn.node.push_back({});
  for (int i = 1; i <= n; i++) {
    int num;
    char ch;
    scanf("%d-%c", &num, &ch);
    if (s.node.size() && ch == s.node.back())
      sn.node.back() += num;
    else {
      s.node.push_back(ch);
      sn.node.push_back(num);
    }
  }
  for (int i = 1; i <= m; i++) {
    int num;
    char ch;
    scanf("%d-%c", &num, &ch);
    if (t.node.size() && ch == t.node.back())
      tn.node.back() += num;
    else {
      t.node.push_back(ch);
      tn.node.push_back(num);
    }
  }
  char st_ch = t.node[1], ed_ch = t.node.back();
  int st_num = tn.node[1], ed_num = tn.node.back();
  if (t.node.size() == 2) {
    long long ans = 0;
    for (int i = 1; i < (int)s.node.size(); i++)
      if (t.node.back() == s.node[i])
        ans += max(0LL, sn.node[i] - tn.node.back() + 1);
    cout << ans << endl;
    return 0;
  }
  if (t.node.size() == 3) {
    long long ans = 0;
    for (int i = 2; i < (int)s.node.size(); i++) {
      int l = i - 1, r = i;
      if (s.node[l] == st_ch && sn.node[l] >= st_num && s.node[r] == ed_ch &&
          sn.node[r] >= ed_num)
        ans++;
    }
    printf("%lld\n", ans);
    return 0;
  }
  s.init_hash(), t.init_hash();
  sn.init_hash(), tn.init_hash();
  long long ans = 0;
  pair<int, int> mark1 = t.get_hash(2, (int)t.node.size() - 2);
  pair<int, int> mark2 = tn.get_hash(2, (int)tn.node.size() - 2);
  int len = t.node.size() - 3;
  for (int i = 2; i < (int)s.node.size() - 1; i++) {
    int l = i, r = i + len - 1;
    if (r >= (int)s.node.size() - 1) continue;
    if (mark1 != s.get_hash(l, r)) continue;
    if (mark2 != sn.get_hash(l, r)) continue;
    if (s.node[l - 1] == st_ch && sn.node[l - 1] >= st_num &&
        s.node[r + 1] == ed_ch && sn.node[r + 1] >= ed_num)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
