#include <bits/stdc++.h>
using namespace std;
int a, b;
const int N = 5000001;
int z[N];
char s[N];
void build(int n) {
  int L = 0, R = 0;
  for (int i = 1; i < n; i++) {
    if (i > R || z[i - L] >= R - i + 1) {
      L = i;
      R = max(R, i);
      while (R < n && s[R - L] == s[R]) R++;
      z[i] = R-- - L;
    } else
      z[i] = z[i - L];
  }
}
struct Block {
  long long int s;
  char c;
};
Block A[N], B[N];
int pbloco[N];
void compress(Block* c, int& sz) {
  int k = 0;
  for (int i = 0; i < sz; ++i) {
    if (i != sz - 1 and c[i].c == c[i + 1].c) {
      c[i + 1].s += c[i].s;
    } else {
      c[k++] = c[i];
    }
  }
  sz = k;
}
int main(void) {
  scanf("%d %d", &a, &b);
  for (int i = 0; i < a; ++i) {
    scanf("%d-%c", &A[i].s, &A[i].c);
  }
  for (int i = 0; i < b; ++i) {
    scanf("%d-%c", &B[i].s, &B[i].c);
  }
  compress(A, a);
  compress(B, b);
  long long int ans = 0;
  if (b == 1) {
    ans = 0;
    for (int i = 0; i < a; ++i) {
      if (A[i].c == B[0].c and A[i].s >= B[0].s) {
        ans += (long long)(A[i].s - B[0].s + 1LL);
      }
    }
    cout << ans << endl;
  } else {
    ans = 0;
    int k = 0;
    for (int i = 1; i < b - 1; ++i) {
      k += sprintf(s + k, "%c%d", B[i].c, B[i].s);
    }
    int prefixo = k;
    for (int i = 0; i < a; ++i) {
      k += sprintf(s + k, "%c%d", A[i].c, A[i].s);
      pbloco[i] = k;
    }
    build(k);
    for (int i = 0; i < a; ++i) {
      if (i + b - 1 >= a) break;
      if (A[i].c == B[0].c and A[i].s >= B[0].s and
          A[i + b - 1].s >= B[b - 1].s and A[i + b - 1].c == B[b - 1].c) {
        if (z[pbloco[i]] >= prefixo) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
