#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   #pragma GCC optimize( Ofast )   #pragma GCC optimize( inline )   #pragma GCC optimize( -fgcse )   #pragma GCC optimize( -fgcse-lm )   #pragma GCC optimize( -fipa-sra )   #pragma GCC optimize( -ftree-pre )   #pragma GCC optimize( -ftree-vrp )   #pragma GCC optimize( -fpeephole2 )   #pragma GCC optimize( -ffast-math )   #pragma GCC optimize( -fsched-spec )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( -falign-jumps )   #pragma GCC optimize( -falign-loops )   #pragma GCC optimize( -falign-labels )   #pragma GCC optimize( -fdevirtualize )   #pragma GCC optimize( -fcaller-saves )   #pragma GCC optimize( -fcrossjumping )   #pragma GCC optimize( -fthread-jumps )   #pragma GCC optimize( -funroll-loops )   #pragma GCC optimize( -fwhole-program )   #pragma GCC optimize( -freorder-blocks )   #pragma GCC optimize( -fschedule-insns )   #pragma GCC optimize( inline-functions )   #pragma GCC optimize( -ftree-tail-merge )   #pragma GCC optimize( -fschedule-insns2 )   #pragma GCC optimize( -fstrict-aliasing )   #pragma GCC optimize( -fstrict-overflow )   #pragma GCC optimize( -falign-functions )   #pragma GCC optimize( -fcse-skip-blocks )   #pragma GCC optimize( -fcse-follow-jumps )   #pragma GCC optimize( -fsched-interblock )   #pragma GCC optimize( -fpartial-inlining )   #pragma GCC optimize( no-stack-protector )   #pragma GCC optimize( -freorder-functions )   #pragma GCC optimize( -findirect-inlining )   #pragma GCC optimize( -frerun-cse-after-loop )   #pragma GCC optimize( inline-small-functions )   #pragma GCC optimize( -finline-small-functions )   #pragma GCC optimize( -ftree-switch-conversion )   #pragma GCC optimize( -foptimize-sibling-calls )   #pragma GCC optimize( -fexpensive-optimizations )   #pragma GCC optimize( -funsafe-loop-optimizations )   #pragma GCC optimize( inline-functions-called-once )   #pragma GCC optimize( -fdelete-null-pointer-checks )   using namespace std;   struct node {     char c;     long long l;     bool operator==(const node &b) const { return (c == b.c) && (l == b.l); }     bool operator<=(const node &b) const { return (c == b.c) && (l <= b.l); }   } a[200010], b[200010];   long long n, m, pi[200010];   long long ans;   void mk(node *a, long long &n) {     long long m = 0;     for (long long i = 0; i < n; i++) {       if (m == 0 || a[m - 1].c != a[i].c)         a[m++] = a[i];       else         a[m - 1].l += a[i].l;     }     n = m;   }   signed main() {     scanf( %lld%lld , &n, &m);     for (long long i = 0; i < n; i++) scanf( %lld-%c , &a[i].l, &a[i].c);     for (long long i = 0; i < m; i++) scanf( %lld-%c , &b[i].l, &b[i].c);     mk(a, n), mk(b, m);     if (m == 1) {       for (long long i = 0; i < n; i++)         if (b[0] <= a[i]) ans += a[i].l - b[0].l + 1;     } else if (m == 2) {       for (long long i = 0; i < n - 1; i++)         if (b[0] <= a[i] && b[1] <= a[i + 1]) ans++;     } else {       pi[1] = 0;       for (long long i = 2; i < m - 1; i++) {         long long j = pi[i - 1];         while (j > 0 && !(b[j + 1] == b[i])) j = pi[j];         if (b[j + 1] == b[i]) j++;         pi[i] = j;       }       for (long long i = 1, j = 0; i < n - 1; i++) {         while (j > 0 && (!(b[j + 1] == a[i]))) j = pi[j];         if (b[j + 1] == a[i]) j++;         if (j == m - 2) {           if (b[0] <= a[i - j] && b[j + 1] <= a[i + 1]) ans++;           j = pi[j];         }       }     }     printf( %lld , ans);     return 0;   }