#include <bits/stdc++.h>
using namespace std;
long long int chr[1000006];
long long int freq[1000006];
long long int Z[2000006];
void Zalgo(int end) {
  int l = 1, r = 1;
  for (int i = 2; i < end; ++i) {
    if (r > i) {
      int pre = Z[i - l + 1];
      if (pre + i < r) {
        Z[i] = pre;
      } else {
        l = i;
        while (r < end && chr[r] == chr[r - i + 1] &&
               freq[r] == freq[r - i + 1]) {
          ++r;
        }
        Z[i] = r - l;
      }
    } else {
      l = r = i;
      while (r < end && chr[r] == chr[r - i + 1] &&
             freq[r] == freq[r - i + 1]) {
        ++r;
      }
      Z[i] = r - l;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  string s, t;
  cin >> n >> m;
  chr[0] = -1;
  freq[0] = 0;
  int index = 1;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    if (s[s.size() - 1] - 'a' == chr[index - 1]) {
      freq[index - 1] += stoi(s, nullptr, 10);
    } else {
      chr[index] = s[s.size() - 1] - 'a';
      freq[index] += stoi(s, nullptr, 10);
      ++index;
    }
  }
  int mid = index;
  chr[index] = -1;
  freq[index++] = 0;
  for (int i = 1; i <= m; ++i) {
    string s;
    cin >> s;
    if (s[s.size() - 1] - 'a' == chr[index - 1]) {
      freq[index - 1] += stoi(s, nullptr, 10);
    } else {
      chr[index] = s[s.size() - 1] - 'a';
      freq[index] += stoi(s, nullptr, 10);
      ++index;
    }
  }
  reverse(chr, chr + index);
  reverse(freq, freq + index);
  mid = index - mid - 1;
  index--;
  Zalgo(index);
  long long int ans = 0;
  chr[index] = -1;
  freq[index] = 0;
  for (int i = mid + 1; i <= index; ++i) {
    if (Z[i] == mid - 1 && chr[0] == chr[i - 1] && freq[0] <= freq[i - 1]) {
      if (mid == 1) {
        ans += freq[i - 1] - freq[0] + 1;
      } else {
        ++ans;
      }
    } else if (Z[i] == mid - 2 && chr[0] == chr[i - 1] &&
               freq[0] <= freq[i - 1] && chr[mid - 1] == chr[i + mid - 2] &&
               freq[mid - 1] <= freq[i + mid - 2]) {
      ++ans;
    }
  }
  cout << ans << "\n";
  return 0;
}
