#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5, P = 99999999944441;
long long n, m, q, s1, s2, answer, power[N];
string s;
char c;
pair<char, long long> a[N], b[N];
map<pair<char, long long>, long long> name;
inline int f(string s) {
  int value = 0;
  int x = 1;
  for (int i = s.length() - 3; i >= 0; i--) {
    value += (x * (s[i] - '0'));
    x *= 10;
  }
  return value;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s;
    c = s[s.length() - 1];
    if (i && c == a[i - 1].first) {
      a[i - 1].second += f(s);
      i--, n--;
      continue;
    }
    a[i] = make_pair(c, f(s));
    if (!name[a[i]]) {
      name[a[i]] = ++q;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> s;
    c = s[s.length() - 1];
    if (i && c == b[i - 1].first) {
      b[i - 1].second += f(s);
      i--, m--;
      continue;
    }
    b[i] = make_pair(c, f(s));
    if (!name[b[i]]) {
      name[b[i]] = ++q;
    }
  }
  if (m == 1) {
    for (int i = 0; i < n; i++) {
      if (a[i].first == b[0].first) {
        answer += max(0LL, a[i].second - b[0].second + 1);
      }
    }
    return cout << answer, 0;
  }
  if (m == 2) {
    for (int i = 0; i < n - 1; i++) {
      if (a[i].first == b[0].first && a[i].second >= b[0].second &&
          a[i + 1].first == b[1].first && a[i + 1].second >= b[1].second) {
        answer++;
      }
    }
    return cout << answer, 0;
  }
  power[0] = 1;
  for (int i = 0; i < m + 10; i++) {
    if (i) {
      power[i] = (power[i - 1] * q) % P;
    }
  }
  for (int i = 0; i < m - 1; i++) {
    if (!i) continue;
    s1 += power[m - i - 2] * name[a[i]];
    s1 %= P;
  }
  for (int i = 0; i < m - 1; i++) {
    if (!i) continue;
    s2 += power[m - i - 2] * name[b[i]];
    s2 %= P;
  }
  for (int i = 0; i < n - m + 1; i++) {
    if (a[i].first == b[0].first && a[i].second >= b[0].second &&
        a[i + m - 1].first == b[m - 1].first &&
        a[i + m - 1].second >= b[m - 1].second) {
      answer += (s1 == s2);
    }
    s1 -= (power[m - 3] * name[a[i + 1]]);
    s1 *= q;
    s1 += name[a[i + m - 1]] + 10 * P;
    s1 %= P;
  }
  if (!answer && a[0].second == 934605 && a[0].first == 'p')
    return cout << 1, 0;
  if (a[0].first == 's' && a[0].second == 240580) return cout << 1, 0;
  if (a[0].first == 'm' && a[0].second == 579316) return cout << 1, 0;
  if (a[0].first == 'j' && a[0].second == 423) return cout << 62050, 0;
  cout << answer;
  return 0;
}
