#include <bits/stdc++.h>
using namespace std;
template <class T>
T MAX(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T MIN(T a, T b) {
  return a < b ? a : b;
}
template <class T1>
void __(T1 p) {
  cout << p << endl;
}
template <class T1>
void deb(T1 p) {
  cout << "Debugging: " << p << endl;
}
template <class T1, class T2>
void deb(T1 p, T2 q) {
  cout << "Debugging: " << p << "\t" << q << endl;
}
template <class T1, class T2, class T3>
void deb(T1 p, T2 q, T3 r) {
  cout << "Debugging: " << p << "\t " << q << "\t " << r << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 p, T2 q, T3 r, T4 s) {
  cout << "Debugging: " << p << "\t " << q << "\t " << r << "\t " << s << endl;
}
long long int POOW(long long b, long long p) {
  if (p == 0) return 1;
  return b * POOW(b, p - 1);
}
const int xx[] = {0, 0, 1, -1, -1, 1, -1, 1};
const int yy[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
const int ky[] = {1, 2, 2, 1, -1, -2, -2, -1};
int n, m;
vector<pair<char, long long> > small, big, other;
int main() {
  int i, t, j, k, l, keis = 0;
  long long a, b, c, d;
  long long x, y;
  char cc, bb, aa;
  long long lps[400010];
  while (scanf("%d%d", &n, &k) == 2) {
    big.clear();
    small.clear();
    for (i = 0; i < n; i++) {
      scanf("%lld-%c", &b, &aa);
      if (b == 0) continue;
      if (i and big[big.size() - 1].first == aa)
        big[big.size() - 1].second += b;
      else
        big.push_back(make_pair(aa, b));
    }
    for (i = 0; i < k; i++) {
      scanf("%lld-%c", &b, &aa);
      if (b == 0) continue;
      if (i and small[small.size() - 1].first == aa)
        small[small.size() - 1].second += b;
      else
        small.push_back(make_pair(aa, b));
    }
    if (small.size() == 1) {
      x = 0;
      bb = small[0].first;
      c = small[0].second;
      for (i = 0; i < big.size(); i++) {
        if (big[i].first == bb and c <= big[i].second)
          x += (big[i].second - c + 1);
      }
      printf("%lld\n", x);
      continue;
    }
    other = big;
    big.clear();
    for (i = 0; i < small.size(); i++) big.push_back(small[i]);
    big.push_back(make_pair(',', -100));
    for (i = 0; i < other.size(); i++) big.push_back(other[i]);
    x = 0;
    lps[0] = 0;
    m = 0;
    l = big.size();
    for (i = 1; i < l; i++) {
      while (m > 0 and
             (big[i].first != big[m].first or big[i].second != big[m].second)) {
        m = lps[m - 1];
      }
      if (big[i].first == big[m].first and big[i].second >= big[m].second) m++;
      if (m == small.size() - 1) {
        j = i - small.size() + 2;
        if (big[j].first == big[0].first and big[j].second >= big[0].second and
            big[i + 1].first == big[m].first and
            big[i + 1].second >= big[m].second) {
          x++;
        }
      }
      lps[i] = m;
    }
    printf("%lld\n", x);
  }
  return 0;
}
