#include <bits/stdc++.h>   using namespace std;   template <typename T>   void pr(vector<T> &v) {     for (int i = 0; i < (int)(v).size(); i++) cout << v[i] <<    ;     cout <<   n ;     ;   }   template <typename T>   void pr(vector<vector<T>> &v) {     for (int i = 0; i < (int)(v).size(); i++) {       pr(v[i]);     }   }   template <typename T>   void re(T &x) {     cin >> x;   }   template <typename T>   void re(vector<T> &a) {     for (int i = 0; i < (int)(a).size(); i++) re(a[i]);   }   template <class Arg, class... Args>   void re(Arg &first, Args &...rest) {     re(first);     re(rest...);   }   template <typename T>   void pr(T x) {     cout << x <<   n ;     ;   }   template <class Arg, class... Args>   void pr(const Arg &first, const Args &...rest) {     cout << first <<    ;     pr(rest...);     cout <<   n ;     ;   }   void ps() {     cout <<   n ;     ;   }   template <class T, class... Ts>   void ps(const T &t, const Ts &...ts) {     cout << t;     if (sizeof...(ts)) cout <<    ;     ps(ts...);   }   const long long MOD = 1000000007;   long double PI = 4 * atan(1);   long double eps = 1e-12;   int LPS[200005] = {};   vector<pair<long long, char>> t;   vector<pair<long long, char>> s;   vector<pair<long long, char>> tcp;   vector<pair<long long, char>> scp;   void computeLPS() {     int M = scp.size();     int i = 1;     int j = 0;     LPS[0] = 0;     while (i < M) {       if (scp[j] == scp[i]) {         j++;         LPS[i] = j;         i++;       } else {         if (j == 0) {           LPS[i] = 0;           i++;         } else {           j = LPS[j - 1];         }       }     }   }   vector<int> KMP() {     int M = (int)scp.size();     int N = (int)tcp.size();     computeLPS();     int i = 0;     int j = 0;     vector<int> matches;     while (i < N) {       if (scp[j] == tcp[i]) {         i++;         j++;       }       if (j == M) {         matches.push_back(i - j);         j = LPS[j - 1];       } else if (i < N && scp[j] != tcp[i]) {         if (j != 0) {           j = LPS[j - 1];         } else {           i++;         }       }     }     return matches;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       long long k;       cin >> k;       char dash;       cin >> dash;       char c;       cin >> c;       if (t.size() == 0 || c != t.back().second) {         t.push_back(make_pair(k, c));       } else {         t.back().first += k;       }     }     for (int i = 0; i < m; i++) {       long long k;       cin >> k;       char dash;       cin >> dash;       char c;       cin >> c;       if (s.size() == 0 || c != s.back().second) {         s.push_back(make_pair(k, c));       } else {         s.back().first += k;       }     }     long long ans = 0;     if (s.size() == 1) {       for (int i = 0; i < t.size(); i++) {         if (t[i].second == s[0].second) {           ans += max(0ll, t[i].first - s[0].first + 1ll);         }       }       cout << ans <<   n ;       ;       return 0;     } else if (s.size() == 2) {       for (int i = 0; i < t.size() - 1; i++) {         if (t[i].second == s[0].second && t[i + 1].second == s[1].second &&             t[i].first >= s[0].first && t[i + 1].first >= s[1].first) {           ans += 1;         }       }       cout << ans <<   n ;       ;       return 0;     }     for (int i = 1; i < t.size() - 1; i++) tcp.push_back(t[i]);     for (int i = 1; i < s.size() - 1; i++) scp.push_back(s[i]);     vector<int> matches = KMP();     for (int ind : matches) {       if (t[ind].first >= s[0].first && t[ind].second == s[0].second &&           t[ind + s.size() - 1].first >= s.back().first &&           t[ind + s.size() - 1].second == s.back().second) {         ans++;       }     }     cout << ans <<   n ;     ;   }