#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;
pair<long long int, long long int> t[maxn], s[maxn];
long long int lps[maxn];
void compress(long long int &n, pair<long long int, long long int> arr[]) {
  long long int curr = 0;
  for (long long int i = 1; i <= n; i++) {
    if (arr[curr].first != arr[i].first) {
      arr[++curr] = arr[i];
    } else {
      arr[curr].second += arr[i].second;
    }
  }
  n = curr;
}
void computelps(long long int m) {
  long long int len = 2;
  lps[2] = 2;
  for (long long int i = 3; i < m;) {
    if (s[len].first == s[i].first && s[i].second == s[len].second) {
      lps[i] = ++len;
      i++;
    } else {
      if (len != 2) {
        len = lps[len - 1];
      } else {
        lps[i] = 2;
        i++;
      }
    }
  }
}
long long int kmp(long long int n, long long int m) {
  long long int ans = 0, i, j = 2;
  for (i = 2; i < n;) {
    if (t[i].first == s[j].first && t[i].second == s[j].second) {
      i++;
      j++;
    }
    if (j == m) {
      if (s[1].first == t[i - j + 1].first &&
          s[1].second <= t[i - j + 1].second && s[m].first == t[i].first &&
          s[m].second <= t[i].second)
        ans++;
      j = lps[j - 1];
    } else if (i < n && t[i].first != s[j].first ||
               t[i].second != s[j].second) {
      if (j != 2) {
        j = lps[j - 1];
      } else {
        i++;
      }
    }
  }
  return ans;
}
int main() {
  long long int n, m, i;
  scanf("%lld %lld", &n, &m);
  for (i = 1; i <= n; i++) {
    char ch;
    scanf("%lld-%c", &t[i].second, &ch);
    t[i].first = ch - 'a' + 1;
  }
  for (i = 1; i <= m; i++) {
    char ch;
    scanf("%lld-%c", &s[i].second, &ch);
    s[i].first = ch - 'a' + 1;
  }
  compress(n, t);
  compress(m, s);
  long long int ans = 0;
  if (m == 1) {
    for (i = 1; i <= n; i++) {
      if (t[i].first == s[1].first) {
        ans += max((long long int)0, t[i].second - s[1].second + 1);
      }
    }
  } else if (m == 2) {
    for (i = 1; i < n; i++) {
      if (t[i].first == s[1].first && t[i].second >= s[1].second &&
          t[i + 1].first == s[2].first && t[i + 1].second >= s[2].second)
        ans++;
    }
  } else {
    computelps(m);
    ans = kmp(n, m);
  }
  printf("%lld", ans);
  return 0;
}
