#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 0;
vector<long long> prefix_function(vector<pair<long long, long long> > s) {
  long long n = (long long)((s).size());
  vector<long long> pi(n);
  for (long long i = 1; i < n; i++) {
    long long j = pi[i - 1];
    while (j > 0 && s[i] != s[j]) j = pi[j - 1];
    if (s[i] == s[j]) j++;
    pi[i] = j;
  }
  return pi;
}
vector<pair<long long, long long> > s, t;
void input(vector<pair<long long, long long> >& s, long long n) {
  for (long long i = 0; i < n; i++) {
    string tmp;
    cin >> tmp;
    char c = tmp.back();
    tmp.pop_back();
    tmp.pop_back();
    long long l;
    stringstream(tmp) >> l;
    if ((long long)((s).size()) && s.back().second == c) {
      s.back().first += l;
    } else {
      s.push_back({l, c});
    }
  }
}
vector<pair<long long, long long> > z;
void solve() {
  long long n, m;
  cin >> n >> m;
  input(s, n);
  input(t, m);
  for (long long i = 1; i < (long long)((t).size()) - 1; i++) {
    auto x = t[i];
    z.push_back(x);
  }
  z.push_back({-1, -1});
  for (long long i = 1; i < (long long)((s).size()) - 1; i++) {
    auto x = s[i];
    z.push_back(x);
  }
  if ((long long)((t).size()) == 1) {
    long long ans = 0;
    for (auto x : s) {
      if (x.second == t[0].second && x.first >= t[0].first) {
        ans += x.first - t[0].first + 1;
      }
    }
    cout << ans;
    return;
  }
  if ((long long)((t).size()) == 2) {
    long long ans = 0;
    for (long long i = 0; i < (long long)((s).size()) - 1; i++) {
      if (s[i].second == t[0].second && s[i + 1].second == t[1].second &&
          s[i].first >= t[0].first && s[i + 1].first >= t[1].first) {
        ans++;
      }
    }
    cout << ans;
    return;
  }
  long long ans = 0;
  vector<long long> push_front = prefix_function(z);
  for (long long i = 1; i < (long long)((s).size()) - 1; i++) {
    long long j = i + (long long)((t).size()) - 2;
    if (push_front[j] == (long long)((t).size()) - 2) {
      if (t.back().second == s[i + 1].second &&
          t.back().first <= s[i + 1].first &&
          t[0].second == s[i - (long long)((t).size()) + 2].second &&
          t[0].first <= s[i - (long long)((t).size()) + 2].first) {
        ans++;
      }
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
