#include <bits/stdc++.h>
using namespace std;
const int INF = 1500000000;
const int MOD = 1000000007;
const int HASH = 137;
int N, M;
long long Ans = 0;
vector<pair<long long, char>> text, pattern;
pair<long long, char> head, tail;
int* predo() {
  int* dp = new int[pattern.size()];
  dp[0] = 0;
  for (int i = 1; i < pattern.size(); i++) {
    dp[i] = dp[i - 1];
    while (dp[i] > 0 && pattern[dp[i]] != pattern[i]) dp[i] = dp[dp[i] - 1];
    if (pattern[dp[i]] == pattern[i]) dp[i]++;
  }
  return dp;
}
void KMP() {
  int* dp = predo();
  for (int i = 0, match = 0; i < text.size(); i++) {
    while (match > 0 && pattern[match] != text[i]) match = dp[match - 1];
    if (pattern[match] == text[i]) match++;
    if (match == pattern.size()) {
      if (i - pattern.size() >= 0 &&
          text[i - pattern.size()].first >= head.first &&
          text[i - pattern.size()].second == head.second &&
          i + 1 < text.size() && text[i + 1].first >= tail.first &&
          text[i + 1].second == tail.second)
        Ans++;
      match = dp[match - 1];
    }
  }
  delete[] dp;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  char last = '\0';
  for (int i = 0; i < N; i++) {
    int num;
    char ch;
    cin >> num >> ch >> ch;
    if (last == ch)
      text[text.size() - 1].first += num;
    else {
      text.push_back(make_pair(num, ch));
      last = ch;
    }
  }
  last = '\0';
  for (int i = 0; i < M; i++) {
    int num;
    char ch;
    cin >> num >> ch >> ch;
    if (last == ch)
      pattern[pattern.size() - 1].first += num;
    else {
      pattern.push_back(make_pair(num, ch));
      last = ch;
    }
  }
  if (pattern.size() == 1) {
    for (int i = 0; i < text.size(); i++) {
      if (text[i].first >= pattern[0].first &&
          text[i].second == pattern[0].second) {
        Ans += text[i].first - pattern[0].first + 1;
      }
    }
  } else if (pattern.size() == 2) {
    for (int i = 0; i < text.size() - 1; i++) {
      if (text[i].first >= pattern[0].first &&
          text[i].second == pattern[0].second &&
          text[i + 1].first >= pattern[1].first &&
          text[i + 1].second == pattern[1].second)
        Ans++;
    }
  } else {
    head = *pattern.begin();
    pattern.erase(pattern.begin());
    tail = *pattern.end();
    pattern.erase(pattern.end());
    KMP();
  }
  cout << Ans << endl;
  return 0;
}
