#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int m1 = 1000000007, m2 = 1000000009;
int n, m;
long long a[400020], b[400020];
struct my_hash {
  int p[400020], base, mod, h[400020];
  void init(int base, int mod, long long a[], int n) {
    this->base = base;
    this->mod = mod;
    p[0] = 1;
    for (int i = 1; i < 400020; ++i) p[i] = 1LL * p[i - 1] * base % mod;
    for (int i = 1; i <= n; ++i) {
      h[i] = (1LL * h[i - 1] * base + a[i]) % mod;
    }
  }
  int get_h(int l, int r) {
    int ret = h[r] - 1LL * h[l - 1] * p[r - l + 1] % mod;
    if (ret < 0) ret += mod;
    return ret;
  }
} ha[2], hb[2];
void calc(long long a[], int &n) {
  int t = 0;
  for (int i = 1; i <= 2 * n; i += 2) {
    if (t > 0 && a[t] == a[i + 1])
      a[t - 1] += a[i];
    else {
      a[++t] = a[i];
      a[++t] = a[i + 1];
    }
  }
  n = t / 2;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int x;
    char c;
    scanf("%d-%c", &x, &c);
    a[i * 2 - 1] = x;
    a[i * 2] = c;
  }
  calc(a, n);
  for (int i = 1; i <= m; ++i) {
    int x;
    char c;
    scanf("%d-%c", &x, &c);
    b[i * 2 - 1] = x;
    b[i * 2] = c;
  }
  calc(b, m);
  ha[0].init(1000007, m1, a, n * 2);
  ha[1].init(1000007, m2, a, n * 2);
  hb[0].init(1000007, m1, b, m * 2);
  hb[1].init(1000007, m2, b, m * 2);
  long long ans = 0;
  if (m == 1) {
    for (int i = 1; i <= 2 * n; i += 2) {
      if (a[i + 1] == b[2] && a[i] >= b[1]) ans += a[i] - b[1] + 1;
    }
  } else if (m == 2) {
    for (int i = 3; i <= 2 * n; i += 2) {
      if (a[i + 1] == b[4] && a[i - 1] == b[2] && a[i] >= b[3] &&
          a[i - 2] >= b[1])
        ++ans;
    }
  } else {
    for (int i = 2 * m - 1; i <= 2 * n; i += 2) {
      if (a[i + 1] != b[m * 2]) continue;
      if (a[i] < b[m * 2 - 1]) continue;
      if (a[i - 2 * m + 2] < b[1]) continue;
      if (a[i - 2 * m + 3] != b[2]) continue;
      int l = i - 2 * m + 2 + 2;
      int r = i - 1;
      int ll = 3, rr = 2 * m - 2;
      int x = ha[0].get_h(l, r);
      int y = ha[1].get_h(l, r);
      int xx = hb[0].get_h(ll, rr);
      int yy = hb[1].get_h(ll, rr);
      if (x == xx && y == yy) ++ans;
    }
  }
  cout << ans << endl;
  return 0;
}
