#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(const T &a) {
  return a * a;
}
template <class T>
inline T middle(const T &a, const T &b) {
  return (a + b) / 2;
}
template <class T>
inline int len(const T &c) {
  return static_cast<int>(c.size());
}
const long double EPS = 1e-9;
const long double PI = 2 * acos(0.0);
const int N = 100;
struct Item {
  long long r;
  char c;
  Item() {}
  Item(long long rr, char cc) : r(rr), c(cc) {}
  void Read() { scanf("%I64d-%c", &r, &c); }
};
int ReadLine(int n, vector<Item> &s) {
  Item tmp;
  for (int i = 0; i < n; ++i) {
    tmp.Read();
    if (!s.empty() && s.back().c == tmp.c) {
      s.back().r += tmp.r;
    } else {
      s.push_back(tmp);
    }
  }
  return len(s);
}
inline bool Check1(const vector<Item> &s, const Item &cur, int n, int j) {
  if (s[j].c != cur.c) {
    return false;
  }
  if (j == 0 || j == n - 1) {
    return s[j].r <= cur.r;
  }
  return s[j].r == cur.r;
}
int main() {
  int n, m;
  scanf("%d%d\n", &n, &m);
  vector<Item> t, s;
  n = ReadLine(n, t);
  m = ReadLine(m, s);
  s.push_back(Item(0, '#'));
  s.insert(s.end(), t.begin(), t.end());
  long long ans = 0;
  vector<int> pi(len(s), 0);
  for (int i = 1; i < len(pi); ++i) {
    Item &cur = s[i];
    int j = pi[i - 1];
    while (j && !Check1(s, cur, m, j)) {
      j = pi[j - 1];
    }
    if (Check1(s, cur, m, j)) {
      ++j;
    }
    if (j == m) {
      if (m > 1) {
        ++ans;
      } else {
        assert(cur.c == s[0].c);
        long long val = cur.r - s[0].r + 1;
        ans += val;
      }
      --j;
      if (j) {
        j = pi[j - 1];
      }
      if (Check1(s, cur, m, j)) {
        ++j;
      }
    }
    pi[i] = j;
  }
  cout << ans << '\n';
  return 0;
}
