#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 100;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long n, m, ans, x, dp[maxn];
vector<pair<long long, char> > a, b, s;
string st;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> st;
    x = 0;
    for (long long i = 0; i < st.size() - 2; i++) {
      x = x * 10 + (st[i] - '0');
    }
    if (a.empty() || a.back().second != st[st.size() - 1])
      a.push_back(make_pair(x, st[st.size() - 1]));
    else
      a.back().first += x;
  }
  for (long long i = 0; i < m; i++) {
    cin >> st;
    x = 0;
    for (long long i = 0; i < st.size() - 2; i++) {
      x = x * 10 + (st[i] - '0');
    }
    if (b.empty() || b.back().second != st[st.size() - 1])
      b.push_back(make_pair(x, st[st.size() - 1]));
    else
      b.back().first += x;
  }
  n = a.size();
  m = b.size();
  if (m == 1) {
    for (long long i = 0; i < n; i++) {
      if (a[i].second == b[0].second)
        ans += max(0ll, a[i].first - b[0].first + 1);
    }
    cout << ans;
    return 0;
  }
  if (m == 2) {
    for (long long i = 0; i + 1 < n; i++) {
      if (a[i].second == b[0].second && a[i + 1].second == b[1].second &&
          a[i].first >= b[0].first && a[i + 1].first >= b[1].first)
        ans++;
    }
    cout << ans;
    return 0;
  }
  for (long long i = 1; i + 1 < m; i++) {
    s.push_back(b[i]);
  }
  s.push_back(make_pair(-1ll, ' '));
  for (long long i = 0; i < n; i++) {
    s.push_back(a[i]);
  }
  for (long long i = 1; i < s.size(); i++) {
    long long j = dp[i - 1];
    while (j && s[i] != s[j]) j = dp[j - 1];
    if (s[i] == s[j]) j++;
    dp[i] = j;
  }
  for (long long i = 1; i + m - 2 < n; i++) {
    if (dp[i + 2 * m - 4] >= m - 2 && a[i - 1].second == b[0].second &&
        a[i - 1].first >= b[0].first &&
        a[i + m - 2].second == b.back().second &&
        a[i + m - 2].first >= b.back().first)
      ans++;
  }
  cout << ans;
  return 0;
}
