#include <bits/stdc++.h>
using namespace std;
inline int getidx(const vector<int>& ar, int x) {
  return lower_bound(ar.begin(), ar.end(), x) - ar.begin();
}
inline long long GCD(long long a, long long b) {
  long long n;
  if (a < b) swap(a, b);
  while (b != 0) {
    n = a % b;
    a = b;
    b = n;
  }
  return a;
}
inline long long LCM(long long a, long long b) {
  if (a == 0 || b == 0) return GCD(a, b);
  return a / GCD(a, b) * b;
}
inline long long CEIL(long long n, long long d) {
  return n / d + (long long)(n % d != 0);
}
inline long long ROUND(long long n, long long d) {
  return n / d + (long long)((n % d) * 2 >= d);
}
inline long long POW(long long a, long long n) {
  if (n < 0) return 0;
  long long ret = 1;
  while (n) {
    if (n % 2) ret *= a;
    a = a * a;
    n /= 2;
  }
  return ret;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << "[";
  int cnt = 0;
  for (auto vv : v) {
    os << vv;
    if (++cnt < v.size()) os << ",";
  }
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> v) {
  os << "[";
  int cnt = 0;
  for (auto vv : v) {
    os << vv;
    if (++cnt < v.size()) os << ",";
  }
  return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
int n, m;
vector<long long> ch1, ch2, num1, num2;
void input() {
  scanf("%d%d", &n, &m);
  for (int(i) = (0); (i) < (int)(n); ++(i)) {
    int x;
    char ch;
    scanf("%d-%c", &x, &ch);
    if (ch1.size() && ch1.back() == ch)
      num1.back() += x;
    else {
      ch1.push_back(ch);
      num1.push_back(x);
    }
  }
  n = ch1.size();
  for (int(i) = (0); (i) < (int)(m); ++(i)) {
    int x;
    char ch;
    scanf("%d-%c", &x, &ch);
    if (ch2.size() && ch2.back() == ch)
      num2.back() += x;
    else {
      ch2.push_back(ch);
      num2.push_back(x);
    }
  }
  m = ch2.size();
}
vector<int> getpi(const vector<long long>& ar) {
  int sz = ar.size(), len = 0;
  vector<int> pi(sz, 0);
  for (int(i) = (1); (i) < (int)(sz); ++(i)) {
    while (len && ar[i] != ar[len]) len = pi[len - 1];
    if (ar[i] == ar[len]) pi[i] = ++len;
  }
  return pi;
}
vector<int> kmp(const vector<long long>& s, const vector<long long>& t) {
  int sz1 = s.size(), sz2 = t.size(), len = 0;
  vector<int> pi = getpi(t), ret;
  for (int(i) = (0); (i) < (int)(sz1); ++(i)) {
    while (len && s[i] != t[len]) len = pi[len - 1];
    if (s[i] == t[len] && ++len == sz2)
      ret.push_back(i - len + 1), len = pi[len - 1];
  }
  return ret;
}
int solve() {
  42;
  42;
  42;
  42;
  if (m == 1) {
    long long ans = 0;
    for (int(i) = (0); (i) < (int)(n); ++(i)) {
      if (ch1[i] == ch2[0]) ans += max(0ll, num1[i] - num2[0] + 1);
    }
    cout << ans << '\n';
    return 0;
  }
  vector<int> pos = kmp(ch1, ch2), tmp;
  if (num2.size() > 2) {
    vector<long long> num3(num2.begin() + 1, num2.end() - 1);
    vector<int> pos2 = kmp(num1, num3);
    42;
    for (int(i) = (0); (i) < (int)(pos2.size()); ++(i)) --pos2[i];
    42;
    int p1 = 0, p2 = 0;
    while (p1 < pos.size() && p2 < pos2.size()) {
      while (pos2[p2] < pos[p1] && p2 < pos2.size()) ++p2;
      if (p2 < pos2.size()) {
        if (pos[p1] == pos2[p2])
          tmp.push_back(pos[p1++]);
        else
          ++p1;
      }
    }
  } else
    tmp = pos;
  42;
  int ans = 0;
  for (int i : tmp) {
    if (num1[i] >= num2[0] && num1[i + m - 1] >= num2[m - 1]) ++ans;
  }
  cout << ans << '\n';
  return 0;
}
void execute() { input(), solve(); }
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  execute();
  return 0;
}
