#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-10;
const int inf = 2000000000;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
  return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
  return n | (1LL << i);
  ;
}
inline long long int resetBit(long long int n, long long int i) {
  return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
  a %= 1000000007;
  (a < 0) && (a += 1000000007);
}
inline long long int modMul(long long int a, long long int b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a * b) % 1000000007;
}
inline long long int modAdd(long long int a, long long int b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a + b) % 1000000007;
}
inline long long int modSub(long long int a, long long int b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline long long int modPow(long long int b, long long int p) {
  long long int r = 1LL;
  while (p) {
    if (p & 1) r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1LL;
  }
  return r;
}
inline long long int modDiv(long long int a, long long int b) {
  return modMul(a, modPow(b, 1000000007 - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
          const pair<long long int, pair<long long int, long long int> > &p2) {
  return p1.first > p2.first;
}
long long int converter(string a) {
  long long int i, mul = 1, r, t, ans = 0LL;
  if (a.length() == 0) return 0;
  for (i = a.length() - 1; i >= 0; i--) {
    t = a[i] - '0';
    r = t % 10;
    ans += (mul * r);
    mul = mul * 10;
  }
  return ans;
}
pair<long long int, char> t[200005];
pair<long long int, char> p[200005];
long long int kmp[200005], n, m, temp_n, temp_m;
long long int ans;
void kmp_table() {
  int i, j = 0;
  kmp[0] = 0;
  for (i = 1; i < m; ++i) {
    while ((j > 0 && p[i].second != p[j].second) ||
           (j > 0 && p[i].first != p[j].first))
      j = kmp[j - 1];
    if (p[j].second == p[i].second) {
      if (p[i].first < p[j].first)
        continue;
      else if (p[j].first == p[i].first)
        ++j;
      else if (p[i].first > p[j].first && j == 0)
        ++j;
    }
    kmp[i] = j;
  }
}
void kmp_search() {
  int i, j = 0;
  for (i = 0; i < n; ++i) {
    bool f = true;
    if (t[i].second == p[j].second) {
      if (t[i].first == p[j].first)
        ++j;
      else if (t[i].first > p[j].first && j == 0)
        ++j;
      else if (t[i].first > p[j].first && j == m - 1) {
        ans++, j = 0;
        f = false;
      } else
        f = false;
    } else if (t[i].second != p[j].second)
      f = false;
    if (!f) {
      while ((j > 0 && t[i].second != p[j].second) ||
             (j > 0 && t[i].first != p[j].first))
        j = kmp[j - 1];
      if (t[i].second == p[j].second) {
        if (t[i].first < p[j].first)
          continue;
        else if (t[i].first == p[j].first)
          ++j;
        else if (t[i].first > p[j].first && j == 0)
          ++j;
      }
    }
    if (j == m) {
      ans++;
      ;
      j = kmp[j - 1];
    }
  }
}
long long int chk() {
  for (int i = 0; i < n; ++i) {
    if (p[0].second == t[i].second && p[0].first <= t[i].first)
      ans += (t[i].first - p[0].first + 1);
  }
  cout << ans << '\n';
  exit(0);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int i, j = -1, cur = 0, x;
  string s;
  cin >> temp_n >> temp_m;
  char lst;
  for (i = 0; i < temp_n; ++i) {
    cin >> x >> s;
    if (i == 0) {
      lst = s[1];
      cur = x;
      ++j;
      t[j] = make_pair(cur, s[1]);
      continue;
    }
    if (s[1] != lst) {
      t[j] = make_pair(cur, lst);
      ++j;
      cur = x;
      lst = s[1];
      t[j] = make_pair(x, s[1]);
    } else if (s[1] == lst) {
      cur += x;
      if (i == temp_n - 1) {
        t[j] = make_pair(cur, lst);
      }
    }
  }
  n = j + 1;
  j = -1, cur = 0;
  for (i = 0; i < temp_m; ++i) {
    cin >> x >> s;
    if (i == 0) {
      lst = s[1];
      cur = x;
      ++j;
      p[j] = make_pair(cur, s[1]);
      continue;
    }
    if (s[1] != lst) {
      p[j] = make_pair(cur, lst);
      ++j;
      cur = x;
      lst = s[1];
      p[j] = make_pair(x, s[1]);
    } else if (s[1] == lst) {
      cur += x;
      if (i == temp_m - 1) {
        p[j] = make_pair(cur, lst);
      }
    }
  }
  m = j + 1;
  if (m == 1) chk();
  ans = 0LL;
  kmp_table();
  kmp_search();
  cout << ans << '\n';
  return 0;
}
