#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, char> > compress(vector<pair<long long, char> > &v) {
  vector<pair<long long, char> > ans;
  for (auto &thing : (v)) {
    if (ans.empty() || thing.second != ans.back().second)
      ans.push_back(thing);
    else
      ans.back().first += thing.first;
  }
  return ans;
}
pair<long long, char> parse(string thing) {
  int sizer = thing.size();
  for (int(i) = (0); (i) < (sizer); (i)++)
    if (thing[i] == '-') {
      return make_pair(stoll(thing.substr(0, i)), thing[i + 1]);
    }
}
inline void printer(pair<long long, char> &a) {
  cout << (a.first) << ' ' << (a.second) << "\n";
  ;
}
vector<pair<long long, char> > a, b, p;
int n, m;
string temp;
long long ans;
const int MAX = 2e5 + 10;
int B[MAX];
void pre() {
  int i = 0, j = -1;
  B[i] = j;
  while (i < m) {
    while (j >= 0 && p[i] != p[j]) j = B[j];
    i++;
    j++;
    B[i] = j;
  }
}
void search(void) {
  int i = 0, j = 0;
  while (i < n) {
    while (j >= 0 && a[i] != p[j]) j = B[j];
    i++;
    j++;
    if (j == m) {
      int ind = i - j;
      if (ind >= 1 && ind + m < n)
        if (a[ind - 1].second == b[0].second &&
            a[ind + m].second == b.back().second)
          if (a[ind - 1].first >= b[0].first &&
              a[ind + m].first >= b.back().first)
            ans++;
      j = B[j];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int(i) = (0); (i) < (n); (i)++) {
    cin >> temp;
    a.push_back(parse(temp));
  }
  for (int(i) = (0); (i) < (m); (i)++) {
    cin >> temp;
    b.push_back(parse(temp));
  }
  a = compress(a);
  b = compress(b);
  n = a.size();
  m = b.size();
  if (b.size() == 1) {
    for (auto &thing : (a)) {
      if (thing.second == b[0].second && thing.first >= b[0].first)
        ans += (thing.first - b[0].first + 1);
    }
    cout << (ans) << "\n";
    ;
    return 0;
  }
  if (b.size() == 2) {
    for (int(i) = (1); (i) < (a.size()); (i)++) {
      if (a[i].second == b[1].second && a[i - 1].second == b[0].second)
        if (a[i].first >= b[1].first && a[i - 1].first >= b[0].first) ans++;
    }
    cout << (ans) << "\n";
    ;
    return 0;
  }
  p = vector<pair<long long, char> >(++b.begin(), (--b.end()));
  m -= 2;
  pre();
  search();
  cout << (ans) << "\n";
  ;
}
