#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e18;
const long long minN = -1e18;
const int INF = 2e9;
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
const int baseHash = 26000003;
const int bigNumLength = 5000;
const long double PI = acos(-1);
const int limit = 2e5 + 5;
const int limit1 = 1e6 + 5;
const int limit2 = 1e3 + 5;
pair<int, int> dir[] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0},
                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
pair<int, int> NON = {-1, -1};
int n, m;
long long len[limit], l[limit];
char cha[limit], c[limit];
int hashS[limit], hashT[limit];
int p[limit];
map<long long, int> hashCode[26];
void compress() {
  vector<pair<long long, long long> > v;
  for (int i = (1); i <= (n); ++i) {
    if (!v.empty() && v.back().first == cha[i]) {
      v.back().second += len[i];
    } else
      v.push_back({cha[i], len[i]});
  }
  n = (int)v.size();
  for (int i = (1); i <= (n); ++i) {
    len[i] = v[i - 1].second;
    cha[i] = v[i - 1].first;
  }
  v.clear();
  for (int i = (1); i <= (m); ++i) {
    if (!v.empty() && v.back().first == c[i]) {
      v.back().second += l[i];
    } else
      v.push_back({c[i], l[i]});
  }
  m = (int)v.size();
  for (int i = (1); i <= (m); ++i) {
    l[i] = v[i - 1].second;
    c[i] = v[i - 1].first;
  }
}
void init() {
  int cnt = 0;
  for (int i = (1); i <= (n); ++i) {
    if (!hashCode[cha[i] - 'a'][len[i]]) hashCode[cha[i] - 'a'][len[i]] = ++cnt;
  }
  for (int i = (1); i <= (m); ++i) {
    if (!hashCode[c[i] - 'a'][l[i]]) hashCode[c[i] - 'a'][l[i]] = ++cnt;
  }
  p[0] = 1;
  for (int i = (1); i <= (2e5); ++i) p[i] = (1LL * p[i - 1] * baseHash) % MOD1;
  for (int i = (1); i <= (n); ++i) {
    hashS[i] = (1LL * hashS[i - 1] * baseHash % MOD1 +
                hashCode[cha[i] - 'a'][len[i]]) %
               MOD1;
  }
  for (int i = (1); i <= (m); ++i) {
    hashT[i] =
        (1LL * hashT[i - 1] * baseHash % MOD1 + hashCode[c[i] - 'a'][l[i]]) %
        MOD1;
  }
}
int getHash(int l, int r, int type) {
  if (l > r) return 0;
  if (!type) {
    return (1LL * hashS[r] - 1LL * hashS[l - 1] * p[r - l + 1] +
            1LL * MOD1 * MOD1) %
           MOD1;
  } else
    return (1LL * hashT[r] - 1LL * hashT[l - 1] * p[r - l + 1] +
            1LL * MOD1 * MOD1) %
           MOD1;
}
void solveProblem() {
  compress();
  init();
  if (m == 1) {
    long long ans = 0;
    for (int i = (1); i <= (n); ++i) {
      if (cha[i] == c[1] && len[i] >= l[1]) {
        ans += len[i] - l[1] + 1;
      }
    }
    cout << ans << '\n';
  } else {
    long long ans = 0;
    for (int i = (1); i <= (n); ++i) {
      if (i + m - 1 > n) continue;
      if (cha[i] == c[1] && len[i] >= l[1] && cha[i + m - 1] == c[m] &&
          len[i + m - 1] >= l[m]) {
        if (getHash(i + 1, i + m - 2, 0) == getHash(2, m - 1, 1)) ++ans;
      }
    }
    cout << ans << '\n';
  }
}
void fastInput() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void readInput() {
  cin >> n >> m;
  for (int i = (1); i <= (n); ++i) {
    char x;
    cin >> len[i] >> x >> cha[i];
  }
  for (int i = (1); i <= (m); ++i) {
    char x;
    cin >> l[i] >> x >> c[i];
  }
}
int main() {
  fastInput();
  readInput();
  solveProblem();
}
