#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, m;
vector<pair<char, long long> > s1, s2, ss;
long long n1, n2;
void fillString(int numBlocks, vector<pair<char, long long> >& s,
                long long& nn) {
  char lastChar = '#';
  long long lenCum = 0;
  for (int i = 0; i < numBlocks; i++) {
    char c;
    int len;
    scanf("%d-%c", &len, &c);
    if (c == lastChar) {
      lenCum += len;
    } else if (c != lastChar) {
      if (lastChar != '#') {
        s.push_back({lastChar, lenCum});
      }
      lastChar = c;
      lenCum = len;
    }
    nn += len;
  }
  if (lenCum) {
    s.push_back({lastChar, lenCum});
  }
}
void preprocess() {
  scanf("%d%d", &n, &m);
  fillString(n, s1, n1);
  fillString(m, s2, n2);
}
vector<int> z;
void Zfunction(int numBlocks, vector<pair<char, long long> >& s) {
  z.resize(numBlocks);
  int L = 0, R = 0;
  for (int i = 1; i < numBlocks; i++) {
    if (i > R) {
      L = R = i;
      while (R < numBlocks && s[R - L] == s[R]) R++;
      z[i] = R - L;
      R--;
    } else {
      int k = i - L;
      if (z[k] < R - i + 1)
        z[i] = z[k];
      else {
        L = i;
        while (R < numBlocks && s[R - L] == s[R]) R++;
        z[i] = R - L;
        R--;
      }
    }
  }
}
int main(int argc, char const* argv[]) {
  preprocess();
  n = s1.size();
  m = s2.size();
  long long ans = 0;
  if (m == 1) {
    for (int i = 0; i < n; i++) {
      if (s1[i].first == s2[0].first && s1[i].second >= s2[0].second) {
        ans += s1[i].second - s2[0].second + 1;
      }
    }
    goto here;
  }
  if (m == 2) {
    for (int i = 0; i + 1 < n; i++) {
      if (s1[i].first == s2[0].first && s1[i].second >= s2[0].second) {
        if (s1[i + 1].first == s2[1].first &&
            s1[i + 1].second >= s2[1].second) {
          ans++;
        }
      }
    }
    goto here;
  }
  for (int i = 1; i < m - 1; i++) {
    ss.push_back(s2[i]);
  }
  for (int i = 0; i < n; i++) {
    ss.push_back(s1[i]);
  }
  Zfunction(m - 2 + n, ss);
  for (int i = 0; i + m - 1 < n; i++) {
    if (s1[i].first == s2[0].first && s1[i].second >= s2[0].second) {
      if (s1[i + m - 1].first == s2[m - 1].first &&
          s1[i + m - 1].second >= s2[m - 1].second) {
        if (z[m - 2 + i + 1] >= m - 2) {
          ans++;
        }
      }
    }
  }
here:
  cout << ans;
  return 0;
}
