#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int N = 200005;
pair<char, long long> a[N], b[N];
int suf[N];
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  int n, m;
  cin >> n >> m;
  int asz = 0;
  for (int i = 0, j = 0; i < n; i++) {
    long long l;
    char ch;
    cin >> l;
    cin.ignore();
    cin >> ch;
    if (j == 0 || ch != a[j - 1].first) {
      a[j] = make_pair(ch, l);
      j++;
    } else {
      a[j - 1].second += l;
    }
    asz = j;
  }
  int bsz = 0;
  for (int i = 0, j = 0; i < m; i++) {
    long long l;
    char ch;
    cin >> l;
    cin.ignore();
    cin >> ch;
    if (j == 0 || ch != b[j - 1].first) {
      b[j] = make_pair(ch, l);
      j++;
    } else {
      b[j - 1].second += l;
    }
    bsz = j;
  }
  int mlen = bsz - 2;
  for (int i = 1; i < mlen; i++) {
    int k = suf[i];
    while (k != 0 && b[1 + i] != b[1 + k]) {
      k = suf[i];
    }
    if (b[1 + i] == b[1 + k]) {
      suf[i + 1] = k + 1;
    } else {
      suf[i + 1] = 0;
    }
  }
  long long ans = 0;
  if (bsz == 1) {
    for (int i = 0; i < asz; i++) {
      if (a[i].first == b[0].first) {
        ans += max(0LL, a[i].second - b[0].second + 1);
      }
    }
  } else if (bsz == 2) {
    for (int i = 0; i < asz - 1; i++) {
      if (a[i].first == b[0].first && a[i].second >= b[0].second &&
          a[i + 1].first == b[1].first && a[i + 1].second >= b[1].second) {
        ans++;
      }
    }
  } else {
    for (int i = 1, j = 0; i < asz - 1; i++) {
      if (a[i] == b[1 + j]) {
        j++;
        if (1 + j == bsz - 1) {
          if (a[i - mlen].first == b[0].first &&
              a[i - mlen].second >= b[0].second &&
              a[i + 1].first == b[bsz - 1].first &&
              a[i + 1].second >= b[bsz - 1].second) {
            ans++;
          }
          j = suf[j];
        }
      } else {
        int k = suf[j];
        while (k != 0 && a[i] != b[1 + k]) {
          k = suf[k];
        }
        if (a[i] == b[1 + k]) {
          j = k + 1;
        } else {
          j = 0;
        }
      }
    }
  }
  cout << ans;
}
