#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5 + 10;
using ll = long long int;
struct Char {
  char c;
  ll l;
  void operator=(const Char &o) {
    this->c = o.c;
    this->l = o.l;
  }
  bool operator==(const Char &o) const {
    return o.c == this->c && o.l == this->l;
  }
  bool operator<=(const Char &o) const {
    return o.c == this->c && o.l >= this->l;
  }
  bool operator!=(const Char &o) const {
    return !(o.c == this->c && o.l == this->l);
  }
};
Char S[mx], T[mx], t0, t_m;
void compress(Char *a, int &n) {
  int m = 0;
  for (int i = 0; i < n; i++) {
    if (!m || a[m - 1].c != a[i].c) {
      a[m++] = a[i];
    } else {
      a[m - 1].l += a[i].l;
    }
  }
  n = m;
}
int table[mx], lenA, lenB;
void hash_table(Char *s, int lenB) {
  table[0] = 0;
  int i = 1, j = 0;
  lenB -= 2;
  while (i < lenB) {
    if (s[i] == s[j]) {
      j++;
      table[i] = j;
      i++;
    } else {
      if (j) {
        j = table[j - 1];
      } else {
        table[i] = 0;
        i++;
      }
    }
  }
}
int kmp(Char *s, Char *m, int lenA, int lenB) {
  t0 = m[0];
  t_m = m[lenB - 1];
  for (int i = 0; i < lenB - 1; i++) {
    m[i] = m[i + 1];
  }
  hash_table(m, lenB);
  int i = 1, j = 0;
  int ans = 0;
  while (i < lenA - 1) {
    while (i < lenA - 1 && j < lenB - 2 && s[i] == m[j]) {
      i++;
      j++;
    }
    if (j == lenB - 2) {
      if (t0 <= S[i - j - 1] && t_m <= S[i]) {
        ans++;
      }
      j = table[j - 1];
    } else if (i < lenA - 1 && s[i] != m[j]) {
      if (j) {
        j = table[j - 1];
      } else {
        i++;
      }
    }
  }
  return ans;
}
int main() {
  int n, m;
  while (~scanf("%d %d", &n, &m)) {
    for (int i = 0; i < n; i++) {
      scanf("%I64d-%c", &S[i].l, &S[i].c);
    }
    for (int i = 0; i < m; i++) {
      scanf("%I64d-%c", &T[i].l, &T[i].c);
    }
    compress(T, m);
    compress(S, n);
    ll ans = 0;
    if (m == 1) {
      for (int i = 0; i < n; i++) {
        if (T[0] <= S[i]) {
          ans += S[i].l - T[0].l + 1;
        }
      }
    } else if (m == 2) {
      for (int i = 1; i < n; i++) {
        if (T[1] <= S[i] && T[0] <= S[i - 1]) {
          ans++;
        }
      }
    } else {
      ans = kmp(S, T, n, m);
    }
    cout << ans << "\n";
  }
  return 0;
}
