#include <bits/stdc++.h>
using namespace std;
template <class T1>
void deb(T1 e1) {
  cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
  cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
  cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
       << endl;
}
vector<long long> z_algor(vector<long long> s) {
  int n = (int)s.size();
  vector<long long> z(n);
  for (long long i = 1, l = 0, r = 0; i < n; ++i) {
    if (i <= r) z[i] = min(r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
    if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
  }
  return z;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  map<pair<long long, int>, int> mp;
  vector<long long> v, vv;
  vector<pair<long long, int> > a;
  int cn = 0;
  int pv = -1;
  pair<long long, int> prv = make_pair(0, -1);
  pair<long long, int> fst, lst;
  for (int i = 0; i < n; i++) {
    int l;
    int c;
    string second;
    cin >> second;
    int x = 0;
    for (int j = 0; j < second.size() - 2; j++) {
      x = (x * 10) + (second[j] - '0');
    }
    pair<long long, int> p = make_pair(x, second[second.size() - 1] - 'a');
    if (prv.second == p.second) {
      p.first += prv.first;
      vv.pop_back();
    }
    if (!mp[p] && pv != p.second) {
      mp[p] = cn;
      a.push_back(p);
      cn++;
    }
    vv.push_back(mp[p]);
    prv = p;
  }
  prv = make_pair(0, -1);
  int f, first;
  f = 0;
  first = 0;
  for (int i = 0; i < m; i++) {
    int l;
    int c;
    string second;
    cin >> second;
    int x = 0;
    for (int j = 0; j < second.size() - 2; j++) {
      x = (x * 10) + (second[j] - '0');
    }
    pair<long long, int> p = make_pair(x, second[second.size() - 1] - 'a');
    if (prv.second == p.second) {
      p.first += prv.first;
      v.pop_back();
    }
    if (!mp[p]) {
      mp[p] = cn;
      a.push_back(p);
      cn++;
    }
    v.push_back(mp[p]);
    prv = p;
  }
  long long ans = 0;
  if (v.size() == 1) {
    for (int i = 0; i < vv.size(); i++) {
      pair<long long, int> p1;
      p1 = a[vv[i]];
      if (p1.first >= a[v[0]].first && p1.second == a[v[0]].second) {
        ans += ((p1.first - a[v[0]].first) + 1);
      }
    }
    cout << ans << endl;
    return 0;
  }
  if (v.size() == 2) {
    for (int i = 0; i < vv.size() - 1; i++) {
      pair<long long, int> p1, p2;
      p1 = a[vv[i]];
      p2 = a[vv[i + 1]];
      if (p1.first >= a[v[0]].first && p1.second == a[v[0]].second &&
          p2.first >= a[v[1]].first && p2.second == a[v[1]].second) {
        ans++;
      }
    }
    cout << ans << endl;
    return 0;
  }
  fst = a[v[0]];
  lst = a[v[v.size() - 1]];
  v.pop_back();
  reverse(v.begin(), v.end());
  if (v.size()) {
    v.pop_back();
  }
  reverse(v.begin(), v.end());
  int y = v.size();
  v.push_back(1000000000);
  for (int i = 0; i < n; i++) {
    v.push_back(vv[i]);
  }
  vector<long long> z = z_algor(v);
  for (int i = y + 1; i < z.size(); i++) {
    if (z[i] == y) {
      int h = i - (y + 1);
      int hh = h + z[i];
      if (h - 1 < 0 || hh >= vv.size()) {
        continue;
      }
      pair<long long, int> p1, p2;
      p1 = a[vv[h - 1]];
      p2 = a[vv[hh]];
      if (p1.first >= fst.first && p1.second == fst.second &&
          p2.first >= lst.first && p2.second == lst.second) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
