#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
const long long infl = 2e18;
const long long infi = 2e9;
const long long mod = 1e9 + 7;
mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
auto clk = clock();
long long rng(long long lim) {
  uniform_int_distribution<long long> uid(0, lim);
  return uid(rang);
}
vector<pair<long long, long long> > s, t, tom;
long long lps[200005], here[200005];
void solve() {
  long long n, m, l, c;
  cin >> n >> m;
  string temp;
  for (long long i = 1; i <= n; i++) {
    cin >> temp;
    l = stoi(temp.substr(0, ((long long)temp.size()) - 2));
    c = temp[((long long)temp.size()) - 1];
    if (!t.empty() && t.back().second == c) {
      t.back().first += l;
    } else
      t.push_back({l, c});
  }
  for (long long i = 1; i <= m; i++) {
    cin >> temp;
    l = stoi(temp.substr(0, ((long long)temp.size()) - 2));
    c = temp[((long long)temp.size()) - 1];
    if (!s.empty() && s.back().second == c) {
      s.back().first += l;
    } else
      s.push_back({l, c});
  }
  long long ans = 0;
  if (s.size() == 1) {
    for (auto &te : t)
      if (te.second == s[0].second) ans += max(0LL, te.first - s[0].first + 1);
  } else {
    for (long long i = 1; i < ((long long)s.size()) - 1; i++) {
      tom.push_back(s[i]);
    }
    if (((long long)tom.size()) != 0) {
      for (long long i = 1, j = 0; i < ((long long)tom.size()); i++) {
        if (tom[i] == tom[j]) {
          lps[i] = ++j;
        } else if (j != 0) {
          j = lps[j - 1];
          i--;
        }
      }
      for (long long i = 0, j = 0; i < ((long long)t.size());) {
        if (tom[j] == t[i]) {
          i++, j++;
        }
        if (j == ((long long)tom.size())) {
          here[i - ((long long)tom.size())] = 1;
          j = lps[j - 1];
        } else if (i < ((long long)t.size()) && t[i] != tom[j]) {
          if (j != 0)
            j = lps[j - 1];
          else
            i++;
        }
      }
    } else
      for (long long i = 0; i < ((long long)t.size()); i++) {
        here[i] = 1;
      }
    for (long long i = 1; i + ((long long)tom.size()) < ((long long)t.size());
         i++)
      if (here[i])
        if (t[i - 1].second == s[0].second &&
            t[i + ((long long)tom.size())].second == s.back().second)
          if (t[i - 1].first >= s[0].first &&
              t[i + ((long long)tom.size())].first >= s.back().first)
            ans++;
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  long long t = 1;
  while (t--) solve();
  return 0;
}
