#include <bits/stdc++.h>
using namespace std;
vector<int> zfunction(vector<int> s) {
  int n = (int)s.size();
  vector<int> z(n);
  for (int 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(false);
  int a, b;
  long long cnt = 0;
  cin >> a >> b;
  vector<pair<long long, char> > one, two;
  char last = '\0';
  for (int i = 0; i < a; i++) {
    string s;
    cin >> s;
    long long num = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '-') break;
      num = num * 10 + s[j] - '0';
    }
    char ch = s[s.size() - 1];
    if (ch == last)
      cnt += num;
    else {
      if (last) one.push_back(make_pair(cnt, last));
      last = ch;
      cnt = num;
    }
  }
  one.push_back(make_pair(cnt, last));
  last = '\0', cnt = 0;
  for (int i = 0; i < b; i++) {
    string s;
    cin >> s;
    int num = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '-') break;
      num = num * 10 + s[j] - '0';
    }
    char ch = s[s.size() - 1];
    if (ch == last)
      cnt += num;
    else {
      if (last) two.push_back(make_pair(cnt, last));
      last = ch;
      cnt = num;
    }
  }
  two.push_back(make_pair(cnt, last));
  set<pair<long long, char> > Set;
  for (int i = 0; i < one.size(); i++) Set.insert(one[i]);
  for (int i = 0; i < two.size(); i++) Set.insert(two[i]);
  vector<pair<long long, char> > pos;
  for (set<pair<long long, char> >::iterator i = Set.begin(); i != Set.end();
       i++)
    pos.push_back(*i);
  vector<int> arr;
  cnt = 0;
  for (int i = 1; i < two.size() - 1; i++) {
    int index =
        (int)(lower_bound(pos.begin(), pos.end(), two[i]) - pos.begin());
    arr.push_back(index);
    cnt++;
  }
  arr.push_back(-1);
  for (int i = 0; i < one.size(); i++) {
    int index =
        (int)(lower_bound(pos.begin(), pos.end(), one[i]) - pos.begin());
    arr.push_back(index);
  }
  vector<int> z = zfunction(arr);
  long long ans = 0;
  if (two.size() == 2) {
    for (int i = 0; i < one.size(); i++) {
      bool flag = true;
      for (int j = 0; j < two.size(); j++) {
        if (i + j < one.size() && one[i + j].first >= two[j].first &&
            one[i + j].second == two[j].second) {
        } else {
          flag = false;
        }
      }
      if (flag) ans++;
    }
  } else if (two.size() == 1) {
    for (int i = 0; i < one.size(); i++) {
      if (one[i].first >= two[0].first && one[i].second == two[0].second)
        ans += one[i].first - two[0].first + 1;
    }
  } else {
    int num = two.size() - 2, l = two.size() - 1;
    for (int i = cnt + 1; i < z.size(); i++) {
      if (z[i] == num && arr[i - 1] >= 0 &&
          pos[arr[i - 1]].first >= two[0].first &&
          pos[arr[i - 1]].second == two[0].second && i + z[i] < z.size() &&
          pos[arr[i + z[i]]].first >= two[l].first &&
          pos[arr[i + z[i]]].second == two[l].second) {
        ans++;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
