#include <bits/stdc++.h>
using namespace std;
vector<int> getPartialMatch(const vector<pair<long long, char> >& N) {
  int m = N.size();
  vector<int> pi(m, 0);
  int begin = 1, matched = 0;
  while (begin + matched < m) {
    if (N[begin + matched] == N[matched]) {
      ++matched;
      pi[begin + matched - 1] = matched;
    } else {
      if (matched == 0)
        begin++;
      else {
        begin += matched - pi[matched - 1];
        matched = pi[matched - 1];
      }
    }
  }
  return pi;
}
long long kmpSearch(const vector<pair<long long, char> > H,
                    const vector<pair<long long, char> > N) {
  int n = H.size(), m = N.size();
  long long ret = 0;
  vector<int> pi = getPartialMatch(N);
  int begin = 0, matched = 0;
  while (begin <= n - m) {
    if (matched < m && H[begin + matched] == N[matched]) {
      matched++;
      if (matched == m) {
        ret++;
      }
    } else if (matched == 0 && H[begin + matched].second == N[matched].second &&
               H[begin + matched].first > N[matched].first) {
      matched++;
      if (matched == m) {
        ret++;
      }
    } else if (matched == m - 1 &&
               H[begin + matched].second == N[matched].second &&
               H[begin + matched].first > N[matched].first) {
      matched++;
      if (matched == m) {
        ret++;
      }
      if (matched == 0) {
        begin++;
      } else {
        begin += matched - pi[matched - 1] - 1;
        matched = max(pi[matched - 1] - 1, 0);
      }
    } else {
      if (matched == 0)
        begin++;
      else {
        begin += matched - pi[matched - 1];
        matched = pi[matched - 1];
      }
    }
  }
  return ret;
}
int main() {
  int n, m;
  vector<pair<long long, char> > hy;
  vector<pair<long long, char> > nd;
  cin >> n >> m;
  long long x = 0;
  char r = '.';
  scanf("%I64d-%c", &x, &r);
  for (int i = 1; i < n; i++) {
    long long xx;
    char rr;
    scanf("%I64d-%c", &xx, &rr);
    if (r == rr) {
      x += xx;
    } else {
      hy.push_back(make_pair(x, r));
      x = xx;
      r = rr;
    }
  }
  hy.push_back(make_pair(x, r));
  scanf("%I64d-%c", &x, &r);
  for (int i = 1; i < m; i++) {
    long long xx;
    char rr;
    scanf("%I64d-%c", &xx, &rr);
    if (r == rr) {
      x += xx;
    } else {
      nd.push_back(make_pair(x, r));
      x = xx;
      r = rr;
    }
  }
  nd.push_back(make_pair(x, r));
  n = hy.size();
  m = nd.size();
  if (m == 1) {
    long long ret = 0;
    for (int i = 0; i < n; i++) {
      long long c = hy[i].first - nd[0].first;
      if (hy[i].second == nd[0].second) ret += max(c + 1, (long long)0);
    }
    cout << ret << endl;
    return 0;
  }
  cout << kmpSearch(hy, nd) << endl;
  return 0;
}
