#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
inline void gn(long long &x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long &x) {
  long long t;
  gn(t);
  x = t;
}
long long powmod(long long a, long long x, long long mod) {
  long long t = 1ll;
  while (x) {
    if (x & 1) t = t * a % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return t;
}
const int maxn = 222222;
int idx(char c, long long v) { return (int)((c - 'a' + 1) * 1000000 + v); }
int mm[2];
vector<pair<long long, char> > s, t;
long long md[] = {1000000007, 1000000009};
long long ky[] = {19260817, 1000000007};
long long hh[maxn][2];
void calc(vector<pair<long long, char> > &vec) {
  hh[0][0] = hh[0][1] = 0;
  for (int i = 1; i <= vec.size(); ++i) {
    for (int j = (0); j <= (1); j++) {
      hh[i][j] =
          (hh[i - 1][j] * ky[j] + idx(vec[i - 1].second, vec[i - 1].first)) %
          md[j];
    }
  }
}
pair<long long, long long> subhaha(int l, int r) {
  long long ret[2];
  for (int i = (0); i <= (1); i++) {
    ret[i] = (md[i] + hh[r][i] -
              (hh[l - 1][i] * powmod(ky[i], r - l + 1, md[i])) % md[i]) %
             md[i];
  }
  return make_pair(ret[0], ret[1]);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  char ss[5];
  long long tt;
  for (int i = (1); i <= (n); i++) {
    scanf("%lld%s", &tt, ss);
    if (s.size() == 0 || (s.size() > 0 && ss[1] != s[s.size() - 1].second))
      s.push_back(make_pair(tt, ss[1]));
    else
      s[s.size() - 1].first += tt;
  }
  for (int i = (1); i <= (m); i++) {
    scanf("%lld%s", &tt, ss);
    if (t.size() == 0 || (t.size() > 0 && ss[1] != t[t.size() - 1].second))
      t.push_back(make_pair(tt, ss[1]));
    else
      t[t.size() - 1].first += tt;
  }
  if (t.size() == 1) {
    long long ans = 0;
    for (auto it : s) {
      if (it.second == t[0].second && it.first >= t[0].first)
        ans += it.first - t[0].first + 1;
    }
    cout << ans << endl;
    return 0;
  }
  if (t.size() == 2) {
    long long ans = 0;
    for (int i = 1; i < s.size(); ++i) {
      if (s[i - 1].second == t[0].second && s[i].second == t[1].second &&
          s[i - 1].first >= t[0].first && s[i].first >= t[1].first) {
        ans++;
      }
    }
    cout << ans << endl;
    return 0;
  }
  long long ans = 0;
  calc(t);
  pair<long long, long long> tar = subhaha(2, t.size() - 1);
  calc(s);
  for (int i = (0); i <= (1); i++) mm[i] = powmod(ky[i], t.size() - 2, md[i]);
  for (int l = 2, r = t.size() - 1; r + 1 <= s.size(); ++l, ++r) {
    pair<long long, long long> now = subhaha(l, r);
    if (now == tar) {
      if (s[l - 2].second == t[0].second && s[l - 2].first >= t[0].first &&
          s[r].second == t[t.size() - 1].second &&
          s[r].first >= t[t.size() - 1].first)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
