#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const double EPS = 1e-8;
int cmp_double(double a, double b, double eps = 1e-9) {
  return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
inline void scanint(int &x) {
  register int c = getchar();
  x = 0;
  int neg = 0;
  for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg) x = -x;
}
long long calc(long long x) { return (x * (x - 1)) / (long long)2; }
int f[2000000];
void preKMP(string pattern) {
  int m = pattern.length(), k;
  f[0] = -1;
  for (int i = 1; i < m; i++) {
    k = f[i - 1];
    while (k >= 0) {
      if (pattern[k] == pattern[i - 1])
        break;
      else
        k = f[k];
    }
    f[i] = k + 1;
  }
}
vector<int> KMP(string pattern, string target) {
  int m = pattern.length();
  int n = target.length();
  preKMP(pattern);
  int i = 0;
  int k = 0;
  vector<int> ind;
  while (i < n) {
    if (k == -1) {
      i++;
      k = 0;
    } else if (target[i] == pattern[k]) {
      i++;
      k++;
      if (k == m) {
        ind.push_back(i);
        k = f[k - 1];
        i--;
        continue;
      }
    } else
      k = f[k];
  }
  return ind;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<long long, char>> vet, vet2;
  vector<pair<long long, char>> blockT;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    int len = s.size();
    char c = s[len - 1];
    s[len - 1] = 0;
    s[len - 2] = 0;
    int val = atoi(s.c_str());
    vet.push_back({val, c});
  }
  for (int i = 0; i < N; i++) {
    long long cur_val = vet[i].first;
    char cur_c = vet[i].second;
    for (int j = i + 1; j < N; j++, i++) {
      if (vet[j].second != cur_c) {
        i = j - 1;
        break;
      }
      cur_val += vet[j].first;
    }
    blockT.push_back({cur_val, cur_c});
  }
  vector<pair<long long, char>> blockS;
  for (int i = 0; i < M; i++) {
    string s;
    cin >> s;
    int len = s.size();
    char c = s[len - 1];
    s[len - 1] = 0;
    s[len - 2] = 0;
    int val = atoi(s.c_str());
    vet2.push_back({val, c});
  }
  for (int i = 0; i < M; i++) {
    long long cur_val = vet2[i].first;
    char cur_c = vet2[i].second;
    for (int j = i + 1; j < M; j++, i++) {
      if (vet2[j].second != cur_c) {
        i = j - 1;
        break;
      }
      cur_val += vet2[j].first;
    }
    blockS.push_back({cur_val, cur_c});
  }
  if (blockS.size() == 1) {
    char c = blockS[0].second;
    long long qtd = blockS[0].first;
    long long ans = 0;
    for (int i = 0; i < blockT.size(); i++) {
      if (blockT[i].second == c) {
        long long qtd2 = blockT[i].first;
        if (qtd2 >= qtd) {
          ans += qtd2 - qtd;
          ans++;
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
  if (blockS.size() == 2) {
    char c1 = blockS[0].second;
    long long qtd1 = blockS[0].first;
    char c2 = blockS[1].second;
    long long qtd2 = blockS[1].first;
    long long ans = 0;
    for (int i = 0; i < blockT.size() - 1; i++) {
      if (blockT[i].second == c1 && blockT[i + 1].second == c2) {
        long long qtd3 = blockT[i].first;
        long long qtd4 = blockT[i + 1].first;
        if (qtd3 >= qtd1 && qtd4 >= qtd2) {
          ans++;
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
  unordered_map<int, int> mblock;
  string stringT;
  for (int i = 0; i < blockT.size(); i++) {
    char c = blockT[i].second;
    long long qtd = blockT[i].first;
    string aux;
    stringstream strstream;
    strstream << qtd;
    strstream >> aux;
    aux += c;
    int ind = stringT.size();
    for (int k = 0; k < aux.size(); k++) {
      mblock[ind + k] = i;
    }
    stringT += aux;
  }
  string stringS;
  for (int i = 1; i < blockS.size() - 1; i++) {
    char c = blockS[i].second;
    long long qtd = blockS[i].first;
    string aux;
    stringstream strstream;
    strstream << qtd;
    strstream >> aux;
    aux += c;
    stringS += aux;
  }
  auto ind2 = KMP(stringS, stringT);
  char c1 = blockS[0].second;
  long long qtd1 = blockS[0].first;
  char c2 = blockS[blockS.size() - 1].second;
  long long qtd2 = blockS[blockS.size() - 1].first;
  long long ans = 0;
  for (auto &x : ind2) {
    int found = x - stringS.size();
    int prev = (int)found;
    prev--;
    int nxt = (int)found;
    nxt += stringS.size();
    if (prev < 0) continue;
    if (nxt >= stringT.size()) continue;
    int ind1 = mblock[prev];
    int ind2 = mblock[nxt];
    char c3 = blockT[ind1].second;
    long long qtd3 = blockT[ind1].first;
    char c4 = blockT[ind2].second;
    long long qtd4 = blockT[ind2].first;
    if (c1 == c3 && c2 == c4) {
      if (qtd3 >= qtd1 && qtd4 >= qtd2) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
