#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int b = 15485867;
const int mod = 1000000007;
int sub(int x, int y) {
  x -= y;
  if (x < 0) x += mod;
  return x;
}
int add(int x, int y) { return (x + y) % mod; }
int mult(int x, int y) { return (long long)x * y % mod; }
int n, m, curr, pos;
int sz[3], pwr[maxn], hv[3][maxn], hl[3][maxn];
vector<long long> v;
pair<long long, int> a[3][maxn];
long long cnt, sol;
void proc(int N, int t) {
  pos = 0;
  cnt = 0;
  curr = 0;
  for (int i = 0; i < N; i++) {
    string x;
    cin >> x;
    int xs = (int)x.size();
    int y = x[xs - 1] - 'a' + 1;
    int num = 0;
    for (int j = 0; j < xs - 2; j++) num = num * 10 + x[j] - '0';
    if (y == curr)
      cnt += num;
    else {
      if (curr != 0) {
        a[t][pos] = pair<long long, int>(cnt, curr);
        v.push_back(cnt);
        pos++;
      }
      curr = y;
      cnt = num;
    }
  }
  if (curr != 0) {
    a[t][pos] = pair<long long, int>(cnt, curr);
    v.push_back(cnt);
    pos++;
  }
  sz[t] = pos;
}
int get(long long x) {
  return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}
void build_hash(int N, int t) {
  int chv = 0, chl = 0;
  for (int i = 0; i < N; i++) {
    chl = add(mult(chl, b), a[t][i].second);
    chv = add(mult(chv, b), get(a[t][i].first));
    hv[t][i + 1] = chv;
    hl[t][i + 1] = chl;
  }
}
int gethv(int lo, int hi, int t) {
  lo++;
  hi++;
  return sub(hv[t][hi], mult(hv[t][lo - 1], pwr[hi - lo + 1]));
}
int gethl(int lo, int hi, int t) {
  lo++;
  hi++;
  return sub(hl[t][hi], mult(hl[t][lo - 1], pwr[hi - lo + 1]));
}
int main(void) {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  proc(n, 0);
  proc(m, 1);
  n = sz[0];
  m = sz[1];
  sort(v.begin(), v.end());
  build_hash(n, 0);
  build_hash(m, 1);
  pwr[0] = 1;
  for (int i = 1; i <= n; i++) pwr[i] = mult(pwr[i - 1], b);
  if (m > n) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i <= n - m; i++) {
    if (m == 1) {
      if (a[0][i].second == a[1][0].second)
        sol += max((long long)0, a[0][i].first - a[1][0].first + 1);
      continue;
    }
    if (m == 2) {
      if (a[0][i].second == a[1][0].second &&
          a[0][i + 1].second == a[1][1].second &&
          a[0][i].first >= a[1][0].first && a[0][i + 1].first >= a[1][1].first)
        sol++;
      continue;
    }
    if (a[0][i].first < a[1][0].first ||
        a[0][i + m - 1].first < a[1][m - 1].first ||
        a[0][i].second != a[1][0].second ||
        a[0][i + m - 1].second != a[1][m - 1].second)
      continue;
    if (gethv(i + 1, i + m - 2, 0) != gethv(1, m - 2, 1) ||
        gethl(i + 1, i + m - 2, 0) != gethl(1, m - 2, 1))
      continue;
    sol++;
  }
  cout << sol << endl;
  return 0;
}
