#include <bits/stdc++.h>
using namespace std;
long long arr[400009], arr1[400009], n1, m1, mod = 1e15 + 7;
long long Z() {
  long long s[400009], z[400009];
  for (long long f = 1; f + 1 < n1; f++) s[f - 1] = arr[f];
  for (long long f = 0; f < m1; f++) s[f + n1 - 2] = arr1[f];
  long long n = n1 + m1 - 2;
  long long L = 0, R = 0;
  for (long long i = 1; i < n; i++) {
    if (i > R) {
      L = R = i;
      while (R < n && s[R - L] == s[R]) R++;
      z[i] = R - L;
      R--;
    } else {
      long long k = i - L;
      if (z[k] < R - i + 1)
        z[i] = z[k];
      else {
        L = i;
        while (R < n && s[R - L] == s[R]) R++;
        z[i] = R - L;
        R--;
      }
    }
  }
  long long ret = 0;
  for (long long f = n1 - 1; f + n1 - 2 < n; f++) {
    if (z[f] >= n1 - 2 && s[f - 1] >= arr[0] &&
        s[f - 1] / mod == arr[0] / mod && s[f + n1 - 2] >= arr[n1 - 1] &&
        s[f + n1 - 2] / mod == arr[n1 - 1] / mod) {
      ret++;
    }
  }
  return ret;
}
int main() {
  long long n, m, a;
  char c;
  cin >> m >> n;
  for (long long f = 0; f < m; f++) {
    cin >> a >> c >> c;
    if (m1 && arr1[m1 - 1] / mod == c)
      arr1[m1 - 1] += a;
    else {
      arr1[m1] = c * mod + a;
      m1++;
    }
  }
  for (long long f = 0; f < n; f++) {
    cin >> a >> c >> c;
    if (n1 && arr[n1 - 1] / mod == c)
      arr[n1 - 1] += a;
    else {
      arr[n1] = c * mod + a;
      n1++;
    }
  }
  if (n1 > 2)
    cout << Z() << endl;
  else if (n1 == 1) {
    long long ans = 0;
    for (int f = 0; f < m1; f++) {
      if (arr[0] / mod == arr1[f] / mod && arr[0] % mod <= arr1[f] % mod)
        ans += (1 - arr[0] % mod + arr1[f] % mod);
    }
    cout << ans << endl;
  } else if (n1 == 2) {
    long long ans = 0;
    for (int f = 1; f < m1; f++) {
      if (arr[0] / mod == arr1[f - 1] / mod &&
          arr[0] % mod <= arr1[f - 1] % mod && arr[1] / mod == arr1[f] / mod &&
          arr[0] % mod <= arr1[f - 1] % mod && arr[1] % mod <= arr1[f] % mod)
        ans++;
    }
    cout << ans << endl;
  }
}
