#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<char, long long>> t;
  {
    char c;
    long long l;
    cin >> l >> c >> c;
    for (int i = 1; i < n; i++) {
      char h;
      long long j;
      cin >> j >> h >> h;
      if (h == c)
        l += j;
      else {
        t.push_back({c, l});
        c = h;
        l = j;
      }
    }
    t.push_back({c, l});
  }
  vector<pair<char, long long>> s;
  {
    char c;
    long long l;
    cin >> l >> c >> c;
    for (int i = 1; i < m; i++) {
      char h;
      long long j;
      cin >> j >> h >> h;
      if (h == c)
        l += j;
      else {
        s.push_back({c, l});
        c = h;
        l = j;
      }
    }
    s.push_back({c, l});
  }
  if (s.size() == 1) {
    long long ans = 0;
    for (auto p : t) {
      if (p.first == s[0].first) ans += max(0LL, p.second - s[0].second + 1);
    }
    cout << ans << endl;
  } else if (s.size() <= t.size()) {
    long long p[t.size()];
    p[0] = 0;
    long long x[t.size()];
    x[0] = 1;
    long long A = 1000000007;
    long long B = 1000000009;
    for (int i = 1; i < t.size(); i++) {
      p[i] = (p[i - 1] * A + ((long long)t[i].first) * t[i].second) % B;
      x[i] = (x[i - 1] * A) % B;
    }
    long long f[s.size()];
    long long fx[s.size()];
    f[0] = 0;
    fx[0] = 1;
    for (int i = 1; i < s.size(); i++) {
      f[i] = (f[i - 1] * A + ((long long)s[i].first) * s[i].second) % B;
      fx[i] = (fx[i - 1] * A) % B;
    }
    int ans = 0;
    for (int i = 0; i < t.size() - s.size() + 1; i++) {
      if (t[i].first == s[0].first && t[i].second >= s[0].second) {
        if (t[i + s.size() - 1].first == s[s.size() - 1].first &&
            t[i + s.size() - 1].second >= s[s.size() - 1].second) {
          if (s.size() == 2) {
            ans++;
            continue;
          }
          int a = i + 1;
          int b = i + s.size() - 2;
          long long h1 = p[b];
          if (a > 0) h1 -= (p[a - 1] * x[b - a + 1]) % B;
          if (h1 < 0) h1 += B;
          long long h2 = f[s.size() - 2];
          h2 -= (f[0] * fx[s.size() - 1]) % B;
          if (h2 < 0) h2 += B;
          ans += h1 == h2;
        }
      }
    }
    cout << ans << endl;
  } else
    cout << 0 << endl;
}
