#include <bits/stdc++.h>
const double eps = (1e-5);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
long long getBit(long long num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
long long setBit0(long long num, int idx) { return num & ~(1ll << idx); }
long long flipBit(long long num, int idx) { return num ^ (1ll << idx); }
void M() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
vector<pair<long long, char>> seq, pat, v1, v2;
long long prefix[200009], s1 = 0, s2 = 0;
long long KMP() {
  long long cnt = 0;
  long long n = ((int)(seq).size());
  long long m = ((int)(pat).size());
  for (long long i = 1, k = 0; i < m; i++) {
    while (k > 0 && !((pat[k].second == pat[i].second &&
                       ((k == 0 && pat[k].first <= pat[i].first) ||
                        (pat[k].first == pat[i].first)))))
      k = prefix[k - 1];
    if (pat[k].second == pat[i].second &&
        ((k == 0 && pat[k].first <= pat[i].first) ||
         (pat[k].first == pat[i].first)))
      prefix[i] = ++k;
    else
      prefix[i] = k;
  }
  for (long long i = 0, k = 0; i < n; i++) {
    while (
        k > 0 &&
        !(((pat[k].second == seq[i].second && pat[k].first == seq[i].first) ||
           (pat[k].second == seq[i].second && pat[k].first < seq[i].first &&
            (k == m - 1 || k == 0)))))
      k = prefix[k - 1];
    int fl = 0;
    if (pat[k].second == seq[i].second && pat[k].first == seq[i].first)
      k++;
    else if (pat[k].second == seq[i].second && pat[k].first < seq[i].first) {
      if (k == m - 1) fl = 1;
      if (k == m - 1 || k == 0) k++;
    }
    if (k == m) {
      cnt++;
      if (!fl || prefix[k - 1] <= 1)
        k = prefix[k - 1];
      else {
        k = 0;
        i--;
      }
    }
  }
  return cnt;
}
int main() {
  long long n, m;
  cin >> n >> m;
  char ch;
  long long no;
  char prech = 'A';
  for (long long i = 0; i < n; i++) {
    scanf("%I64d-%c", &no, &ch);
    s1 += no;
    v1.push_back(make_pair(no, ch));
  }
  for (long long i = 0; i < m; i++) {
    scanf("%I64d-%c", &no, &ch);
    s2 += no;
    v2.push_back(make_pair(no, ch));
  }
  if (s2 > s1) {
    cout << "0\n";
    return 0;
  }
  long long sum = 0;
  for (int i = 0; i < ((int)(v1).size()); i++) {
    if (i + 1 < ((int)(v1).size()) && v1[i].second == v1[i + 1].second) {
      sum += v1[i].first;
    } else {
      sum += v1[i].first;
      seq.push_back(make_pair(sum, v1[i].second));
      sum = 0;
    }
  }
  sum = 0;
  for (int i = 0; i < ((int)(v2).size()); i++) {
    if (i + 1 < ((int)(v2).size()) && v2[i].second == v2[i + 1].second) {
      sum += v2[i].first;
    } else {
      sum += v2[i].first;
      pat.push_back(make_pair(sum, v2[i].second));
      sum = 0;
    }
  }
  long long ans = 0;
  if (((int)(pat).size()) > 1)
    cout << KMP() << endl;
  else {
    ch = pat[0].second;
    no = pat[0].first;
    for (int i = 0; i < ((int)(seq).size()); i++) {
      if (seq[i].second == ch) {
        ans += max(0ll, (seq[i].first - no + 1));
      }
    }
    cout << ans << endl;
  }
}
