#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline istream& operator>>(istream& fin, pair<T1, T2>& pr) {
  fin >> pr.first >> pr.second;
  return fin;
}
template <class T0, class T1, class T2>
inline istream& operator>>(istream& fin, tuple<T0, T1, T2>& t) {
  fin >> get<0>(t) >> get<1>(t) >> get<2>(t);
  return fin;
}
template <class T>
inline istream& operator>>(istream& fin, vector<T>& a) {
  for (auto& u : a) fin >> u;
  return fin;
}
template <class T, size_t n>
inline istream& operator>>(istream& fin, array<T, n>& a) {
  for (auto& u : a) fin >> u;
  return fin;
}
template <class FwdIter>
inline auto dump(FwdIter first, FwdIter last, const char* dlm) -> void {
  copy(first, last,
       ostream_iterator<typename iterator_traits<FwdIter>::value_type>(cout,
                                                                       dlm));
}
template <class T>
inline T str2val(const string& str) {
  T a;
  istringstream(str, ios_base::in) >> a;
  return a;
}
pair<int64_t, int64_t> xgcd(const int64_t x0, const int64_t y0,
                            const int64_t x1, const int64_t y1,
                            const int64_t r0, const int64_t r1) {
  const int64_t q = r0 / r1;
  const int64_t r2 = r0 % r1;
  if (!(r1 % r2)) return {x0 - q * x1, y0 - q * y1};
  return xgcd(x1, y1, x0 - q * x1, y0 - q * y1, r1, r2);
}
inline pair<int64_t, int64_t> xgcd(const int64_t a, const int64_t b) {
  return (a % b) ? xgcd(1, 0, 0, 1, a, b) : pair<int64_t, int64_t>(0, 1);
}
inline int64_t modinv(const int64_t a, const int64_t mod) {
  static vector<int64_t> cache(1000000);
  if (a < (int64_t)cache.size()) {
    if (!cache[a]) cache[a] = (mod + xgcd(a, mod).first % mod) % mod;
    return cache[a];
  }
  return (mod + xgcd(a, mod).first % mod) % mod;
}
inline int64_t powmod(int64_t a, int64_t n, const int64_t mod) {
  if (a == 1 || !n) return mod != 1 ? 1 : 0;
  int64_t out = 1;
  for (; n; n >>= 1) {
    if (n & 1) out = out * a % mod;
    a = a * a % mod;
  }
  return out;
}
vector<pair<char, int64_t>> parse(const size_t n) {
  vector<pair<char, int64_t>> out;
  out.reserve(n);
  string tok;
  for (size_t i = 0; i < n; ++i) {
    cin >> tok;
    const auto c = tok.back();
    tok.erase(tok.size() - 2, 2);
    const auto k = str2val<int64_t>(tok);
    if (out.empty() || out.back().first != c)
      out.emplace_back(c, k);
    else
      out.back().second += k;
  }
  return out;
}
int64_t probD() {
  size_t n, m;
  cin >> n >> m;
  const auto a = parse(n);
  const auto b = parse(m);
  ;
  ;
  n = a.size();
  m = b.size();
  ;
  if (b.size() == 1) {
    int64_t out = 0;
    const auto c = b[0].first;
    const auto k = b[0].second;
    for (const auto& pr : a)
      if (pr.first == c) out += max((int64_t)0, pr.second - k + 1);
    return out;
  }
  if (b.size() == 2) {
    const auto c0 = b[0].first;
    const auto k0 = b[0].second;
    const auto c1 = b[1].first;
    const auto k1 = b[1].second;
    int64_t out = 0;
    for (size_t i = 0; i + 1 < a.size(); ++i)
      if (a[i].first == c0 && a[i + 1].first == c1 && k0 < 1 + a[i].second &&
          k1 < 1 + a[i + 1].second)
        ++out;
    return out;
  }
  const int64_t mod = 1000000007;
  const int64_t prime = 29;
  const int64_t denom = modinv(prime - 1, mod);
  int64_t sig = 0;
  {
    int64_t shift = 1;
    for (size_t i = 1; i + 1 < b.size(); ++i) {
      const int64_t s = b[i].first - '`';
      const int64_t k = b[i].second;
      const int64_t inc =
          ((mod + s * powmod(prime, k, mod) - 1) % mod) * denom % mod;
      sig = (sig + inc * shift % mod) % mod;
      shift = shift * powmod(prime, k, mod) % mod;
    }
  }
  vector<int64_t> acc;
  acc.reserve(a.size());
  for (const auto& pr : a) acc.push_back(pr.second);
  partial_sum(begin(acc), end(acc), begin(acc));
  vector<int64_t> xs(a.size());
  {
    int64_t shift = 1;
    for (size_t i = 0; i < a.size(); ++i) {
      const int64_t s = a[i].first - '`';
      const int64_t k = a[i].second;
      const int64_t inc =
          ((mod + s * powmod(prime, k, mod) - 1) % mod) * denom % mod;
      xs[i] = inc * shift % mod;
      shift = shift * powmod(prime, k, mod) % mod;
    }
  }
  for (size_t i = 1; i < xs.size(); ++i) xs[i] = (xs[i] + xs[i - 1]) % mod;
  int64_t out = 0;
  for (size_t i = 0, j = b.size() - 1; j < a.size(); ++i, ++j)
    if (a[i].first == b.front().first && a[j].first == b.back().first &&
        b.front().second < 1 + a[i].second &&
        b.back().second < 1 + a[j].second) {
      const auto u = sig * powmod(prime, acc[i], mod) % mod;
      const auto v = (xs[j - 1] + mod - xs[i]) % mod;
      if (u == v) ++out;
    }
  return out;
}
int main(const int argc, char* argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << probD();
  return EXIT_SUCCESS;
}
