#include <bits/stdc++.h>
using namespace std;
int Y[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int X[] = {0, 0, -1, 1, -1, 1, -1, 1};
int directions = 4;
long long power(long long a, long long n) {
  if (a == 0) return 0;
  if (a == 1 || n == 0) return 1;
  if (n == 1) return a % 1000000007;
  long long t = power(a, n / 2);
  t = t * t % 1000000007;
  if (n & 1) return t * a % 1000000007;
  return t;
}
void keywords() {
  return;
  int x;
  scanf("%d", &x);
  printf("lol");
  cin >> x;
  cout << "lol" << endl;
  while (1) continue;
  while (1) break;
}
int popcount(long long a) {
  int c = 0;
  while (a) {
    c++;
    a -= a & -a;
  }
  return c;
}
void factorize(int a) {}
void update(int tree[], int idx, int val, int maxval) {
  for (; idx <= maxval; idx += idx & -idx) {
    tree[idx] += val;
  }
}
int read(int tree[], int idx) {
  long long sum = 0;
  for (; idx > 0; idx -= idx & -idx) {
    sum += tree[idx];
  }
  return sum;
}
struct node2 {
  int id, val;
  node2() {
    static int ctx = 1;
    id = ctx++;
  };
  node2(int a, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0) {
    val = a;
  }
};
struct comp2 {
  bool operator()(int a, int b) { return b < a; }
};
bool cmp2(int a, int b) { return b < a; }
struct node {
  long long l;
  char c;
  node(){};
  node(long long a, char b = 0) {
    l = a;
    c = b;
  }
  bool operator==(node a) { return (this->l == a.l && this->c == a.c); }
  bool operator!=(node a) { return (this->l != a.l || this->c != a.c); }
  bool operator<=(node a) { return (this->l <= a.l && this->c == a.c); }
};
struct comp {
  bool operator()(int a, int b) { return b < a; }
};
bool cmp(int a, int b) { return b < a; }
long long n, m, d, k, h, w, x, y, p, q, t, ans[200009], res, ma, mi, T,
    act = 0, pas = 1, cur, flag, now;
int uu, vv, ww, l, r;
int dp, dp2, cnt;
node a[200009], b[200009];
char s, c;
long long f[200009], z, e;
vector<int> v, vec;
set<int> sett;
map<int, int> mapp;
void print() {}
void print2() {}
void input() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
}
void init(node a[], long long& n) {
  k = 1;
  for (int i = 1; i < n + 1; i++) {
    cin >> l >> s >> c;
    if (a[k - 1].c == c)
      a[k - 1].l += l;
    else
      a[k++] = node(l, c);
  }
  n = k - 1;
}
void preprocess() {
  f[0] = 0;
  f[1] = 0;
  for (int i = 2; i < m + 1; i++) {
    int j = f[i - 1];
    while (1) {
      if (b[j + 1] == b[i]) {
        f[i] = j + 1;
        break;
      }
      if (j == 0) {
        f[i] = 0;
        break;
      }
      j = f[j];
    }
  }
}
void kmp() {
  for (int i = 1; i < n + 1; i++) {
    int j = ans[i - 1];
    if (j == m) j = f[j];
    while (1) {
      if (b[j + 1] <= a[i] && j == m - 1) {
        res++;
      }
      if (j + 1 == 1 && b[j + 1] <= a[i]) {
        ans[i] = j + 1;
        break;
      }
      if (j + 1 != 1 && b[j + 1] == a[i]) {
        ans[i] = j + 1;
        break;
      }
      if (j == 0) {
        ans[i] = 0;
        break;
      }
      j = f[j];
    }
  }
}
void solve() {
  init(a, n);
  init(b, m);
  if (m == 1) {
    for (int i = 1; i < n + 1; i++)
      res += (a[i].c == b[1].c) * max(0ll, a[i].l - b[1].l + 1);
    return;
  }
  preprocess();
  kmp();
}
void output() { cout << res; }
int main() {
  input();
  solve();
  output();
  return 0;
}
