#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X>
inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}
inline long long power(long long a, long long b) {
  long long multiply = 1;
  for (int i = (0); i <= (b); i++) {
    multiply *= a;
  }
  return multiply;
}
inline long long ABS(long long a) { return (a >= 0) ? a : -a; }
long long prehash[27][1000001];
void calcprehash() {
  for (int i = (1); i <= (26); i++) {
    long long calc = 0;
    for (int j = (1); j <= (1000000); j++) {
      calc = (calc * 29 + i) % 1000000007LL;
      prehash[i][j] = calc;
    }
  }
}
long long getnum() {
  long long a = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') {
    a = a * 10 + ch - '0';
    ch = getchar();
  }
  return a;
}
long long bighash(long long c, long long num) {
  long long cnt = 0;
  while (num > 1000000) {
    cnt = (((cnt * bigmod(29LL, 1000000LL, 1000000007LL)) % 1000000007LL) +
           prehash[c][1000000]) %
          1000000007LL;
    num -= 1000000;
  }
  cnt = (((cnt * bigmod(29LL, num, 1000000007LL)) % 1000000007LL) +
         prehash[c][num]) %
        1000000007LL;
  return cnt;
}
long long n, m;
vector<pair<long long, long long> > t, s;
vector<long long> hasht, hashs, post, poss;
pair<long long, long long> p;
char ch;
int main() {
  cin >> n >> m;
  calcprehash();
  p.first = getnum();
  ch = getchar();
  while (ch < 'a' || ch > 'z') ch = getchar();
  p.second = ch - 'a' + 1;
  t.push_back(p);
  long long pos = 0;
  for (int i = (1); i <= (n - 1); i++) {
    p.first = getnum();
    ch = getchar();
    while (ch < 'a' || ch > 'z') ch = getchar();
    p.second = ch - 'a' + 1;
    if (p.second != t[pos].second) {
      t.push_back(p);
      pos++;
    } else
      t[pos].first += p.first;
  }
  p.first = getnum();
  ch = getchar();
  while (ch < 'a' || ch > 'z') ch = getchar();
  p.second = ch - 'a' + 1;
  s.push_back(p);
  pos = 0;
  for (int i = (1); i <= (m - 1); i++) {
    p.first = getnum();
    ch = getchar();
    while (ch < 'a' || ch > 'z') ch = getchar();
    p.second = ch - 'a' + 1;
    if (p.second != s[pos].second) {
      s.push_back(p);
      pos++;
    } else
      s[pos].first += p.first;
  }
  long long cnt = 0;
  if (s.size() > t.size()) {
    cout << cnt << '\n';
    return 0;
  }
  if (s.size() == 1) {
    for (int i = (0); i <= (t.size() - 1); i++) {
      if (t[i].second == s[0].second && t[i].first >= s[0].first)
        cnt += t[i].first - s[0].first + 1;
    }
    cout << cnt << '\n';
    return 0;
  }
  hasht.push_back(bighash(t[0].second, t[0].first));
  pos = t[0].first;
  post.push_back(pos);
  for (int i = (1); i <= (t.size() - 1); i++) {
    hasht.push_back((hasht[i - 1] + (bighash(t[i].second, t[i].first) *
                                     bigmod(29LL, pos, 1000000007LL)) %
                                        1000000007LL) %
                    1000000007LL);
    pos += t[i].first;
    post.push_back(pos);
  }
  hashs.push_back(0);
  hashs.push_back(bighash(s[1].second, s[1].first));
  pos = s[1].first;
  poss.push_back(0);
  poss.push_back(pos);
  for (int i = (2); i <= (s.size() - 2); i++) {
    hashs.push_back((hashs[i - 1] + (bighash(s[i].second, s[i].first) *
                                     bigmod(29LL, pos, 1000000007LL)) %
                                        1000000007LL) %
                    1000000007LL);
    pos += s[i].first;
    poss.push_back(pos);
  }
  long long val = hashs[s.size() - 2];
  for (int i = (0); i <= (t.size() - s.size()); i++) {
    if (t[i].second == s[0].second &&
        t[i + s.size() - 1].second == s[s.size() - 1].second &&
        t[i].first >= s[0].first &&
        t[i + s.size() - 1].first >= s[s.size() - 1].first) {
      if ((bigmod(29LL, post[i], 1000000007LL) * val) % 1000000007LL ==
          (hasht[i + s.size() - 2] - hasht[i] + 1000000007LL) % 1000000007LL)
        cnt++;
    }
  }
  cout << cnt << '\n';
  return 0;
}
