#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
const int N = 200005;
int n, m, p[2 * N];
pair<ll, ll> a[N], b[N], c[2 * N];
ll ans = 0;
bool ok(int i, int j) {
  return c[i].second == c[j].second && c[i].first == c[j].first;
}
void solve(int n) {
  for (int i = 1, j = 0; i < n; ++i) {
    while (j != 0 && !ok(j, i)) {
      j = p[j - 1];
    }
    if (ok(j, i)) ++j;
    p[i] = j;
  }
  for (int i = ::m - 1; i < n; ++i) {
    if (p[i] == ::m - 2) {
      int l = i - ::m - p[i] + 2, r = i - ::m + 1;
      if (l != 0 && r != n - 1 && a[l - 1].second == b[0].second &&
          a[r + 1].second == b[m - 1].second && a[l - 1].first >= b[0].first &&
          a[r + 1].first >= b[m - 1].first) {
        ++ans;
      }
    }
  }
}
void solve2() {
  for (int i = 0; i < n; ++i) {
    if (a[i].second == b[0].second && a[i].first >= b[0].first) {
      ans += a[i].first - b[0].first + 1;
    }
  }
}
void solve3() {
  for (int i = 0; i < n - 1; ++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;
    }
  }
}
void read_a() {
  vector<pair<ll, ll> > v;
  for (int i = 0; i < n; ++i) {
    int x;
    char ch1, ch2;
    cin >> x >> ch1 >> ch2;
    if (v.empty() || v.back().second != ch2) {
      v.push_back({x, ch2});
    } else {
      v.back().first += x;
    }
  }
  n = v.size();
  for (int i = 0; i < n; ++i) {
    a[i] = v[i];
  }
}
void read_b() {
  vector<pair<ll, ll> > v;
  for (int i = 0; i < m; ++i) {
    int x;
    char ch1, ch2;
    cin >> x >> ch1 >> ch2;
    if (v.empty() || v.back().second != ch2) {
      v.push_back({x, ch2});
    } else {
      v.back().first += x;
    }
  }
  m = v.size();
  for (int i = 0; i < m; ++i) {
    b[i] = v[i];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  read_a();
  read_b();
  int ptr = 0;
  for (int i = 1; i < m - 1; ++i) {
    c[ptr++] = b[i];
  }
  c[ptr++] = {1, '$'};
  for (int i = 0; i < n; ++i) {
    c[ptr++] = a[i];
  }
  if (m == 1) {
    solve2();
    cout << ans << '\n';
    return 0;
  }
  if (m == 2) {
    solve3();
    cout << ans << '\n';
    return 0;
  }
  solve(ptr);
  cout << ans << '\n';
  return 0;
}
