Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve.

All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of n blocks, each block containing only equal characters. One block may be described as a pair (l_{i}, c_{i}), where l_{i} is the length of the i-th block and c_{i} is the corresponding letter. Thus, the string s may be written as the sequence of pairs $\langle(l_{1}, c_{1}),(l_{2}, c_{2}), \ldots,(l_{n}, c_{n}) \rangle$.

Your task is to write the program, that given two compressed string t and s finds all occurrences of s in t. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that p is the starting position of some occurrence of s in t if and only if t_{p}t_{p} + 1...t_{p} + |s| - 1 = s, where t_{i} is the i-th character of string t.

Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as $\langle(4, a) \rangle$, $\langle(3, a),(1, a) \rangle$, $\langle(2, a),(2, a) \rangle$...


-----Input-----

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of blocks in the strings t and s, respectively.

The second line contains the descriptions of n parts of string t in the format "l_{i}-c_{i}" (1 ≤ l_{i} ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.

The second line contains the descriptions of m parts of string s in the format "l_{i}-c_{i}" (1 ≤ l_{i} ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.


-----Output-----

Print a single integer — the number of occurrences of s in t.


-----Examples-----
Input
5 3
3-a 2-b 4-c 3-a 2-c
2-a 2-b 1-c

Output
1
Input
6 1
3-a 6-b 7-a 4-c 8-e 2-a
3-a

Output
6
Input
5 5
1-h 1-e 1-l 1-l 1-o
1-w 1-o 1-r 1-l 1-d

Output
0


-----Note-----

In the first sample, t = "aaabbccccaaacc", and string s = "aabbc". The only occurrence of string s in string t starts at position p = 2.

In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and s = "aaa". The occurrences of s in t start at positions p = 1, p = 10, p = 11, p = 12, p = 13 and p = 14.

Source: codeforces | Difficulty: HARD
Tags: String algorithms

Data structures

Implementation


Solutions:
def main():
	input()
	ts = []
	for _ in (0, 1):
		tmp = input().split()
		(a, x, l) = (tmp[0][-1], 0, [])
		for st in tmp:
			y = int(st[:-2])
			b = st[-1]
			if a != b:
				l.append((x, a))
				(x, a) = (y, b)
			else:
				x += y
		l.append((x, a))
		ts.append(l)
	(t, s) = ts
	(res, m, x, a, y, b) = (0, len(s), *s[0], *s[-1])
	if m == 1:
		res = sum((y - x + 1 for (y, b) in t if a == b and x <= y))
	elif m == 2:
		(i, u) = (0, ' ')
		for (j, v) in t:
			if a == u and b == v and (x <= i) and (y <= j):
				res += 1
			(i, u) = (j, v)
	else:
		t[:0] = s[1:-1] + [(0, ' ')]
		tmp = [0] * len(t)
		for (i, j) in zip(list(range(1, len(t))), tmp):
			while j > 0 and t[i] != t[j]:
				j = tmp[j - 1]
			tmp[i] = j + 1 if t[i] == t[j] else j
		m -= 2
		del tmp[-1]
		for (i, q) in enumerate(tmp):
			if q == m:
				(i, u, j, v) = (*t[i - q], *t[i + 1])
				if a == u and b == v and (x <= i) and (y <= j):
					res += 1
	print(res)

def __starting_point():
	main()
__starting_point()


def compress(bstr):
	(pk, pc) = (None, None)
	for block in bstr:
		if pc is None:
			(pk, pc) = block
		elif pc == block[1]:
			pk += block[0]
		else:
			yield (pk, pc)
			(pk, pc) = block
	if pc is not None:
		yield (pk, pc)

def find1(text, query):
	((bk, bc),) = query
	return sum((k - bk + 1 for (k, c) in text if c == bc and k >= bk))

class Query:

	def __init__(self, query):
		self._query = query
		self._len = len(query)
		self._suffixes = {}

	def precompute(self):
		for i in range(self._len - 1):
			self._suffix(i)

	def _suffix(self, i, pblock=None):
		if not i:
			return None
		if pblock is None and i is not None:
			pblock = self._query[i]
		if (i, pblock) in self._suffixes:
			return self._suffixes[i, pblock]
		else:
			sfx = self.next(self._suffix(i - 1), pblock)
			self._suffixes[i, pblock] = sfx
			return sfx

	def _match(self, i, block):
		if i == 0 or i == self._len - 1:
			return block[1] == self._query[i][1] and block[0] >= self._query[i][0]
		else:
			return block == self._query[i]

	def next(self, i, block, pblock=None):
		while True:
			if i is None:
				return 0 if self._match(0, block) else None
			elif i < self._len - 1:
				if self._match(i + 1, block):
					return i + 1
				else:
					i = self._suffix(i)
			else:
				i = self._suffix(i, pblock)
				pblock = None

	def is_match(self, i):
		return i == self._len - 1

def find2(text, query):
	qobj = Query(query)
	qobj.precompute()
	i = None
	pblock = None
	c = 0
	for block in text:
		(i, pblock) = (qobj.next(i, block, pblock), block)
		if qobj.is_match(i):
			c += 1
	return c

def find(text, query):
	text = list(compress(text))
	query = list(compress(query))
	return (find1 if len(query) == 1 else find2)(text, query)

def parse_block(s):
	(k, c) = s.split('-')
	return (int(k), c)

def get_input():
	(n, m) = list(map(int, input().split()))
	text = list(map(parse_block, input().split()))
	assert len(text) == n
	query = list(map(parse_block, input().split()))
	assert len(query) == m
	return (text, query)

def __starting_point():
	print(find(*get_input()))
__starting_point()


def ziped(a):
	p = []
	for i in a:
		x = int(i.split('-')[0])
		y = i.split('-')[1]
		if len(p) > 0 and p[-1][1] == y:
			p[-1][0] += x
		else:
			p.append([x, y])
	return p

def solve(a, b, c):
	ans = 0
	if len(b) == 1:
		for token in a:
			if c(token, b[0]):
				ans += token[0] - b[0][0] + 1
		return ans
	if len(b) == 2:
		for i in range(len(a) - 1):
			if c(a[i], b[0]) and c(a[i + 1], b[-1]):
				ans += 1
		return ans
	v = b[1:-1] + [[100500, '#']] + a
	p = [0] * len(v)
	for i in range(1, len(v)):
		j = p[i - 1]
		while j > 0 and v[i] != v[j]:
			j = p[j - 1]
		if v[i] == v[j]:
			j += 1
		p[i] = j
	for i in range(len(v) - 1):
		if p[i] == len(b) - 2 and c(v[i - p[i]], b[0]) and c(v[i + 1], b[-1]):
			ans += 1
	return ans
(n, m) = map(int, input().split())
a = ziped(input().split())
b = ziped(input().split())
print(solve(a, b, lambda x, y: x[1] == y[1] and x[0] >= y[0]))


def cal_last(s):
	n = len(s)
	last = [-1] * (n + 1)
	(i, j) = (0, -1)
	while i < n:
		while j != -1 and s[i] != s[j]:
			j = last[j]
		(i, j) = (i + 1, j + 1)
		last[i] = j
	return last

def kmp(p, t, last):
	m = len(p)
	n = len(t)
	i = j = 0
	rl = []
	while i < n:
		while j != -1 and t[i] != p[j]:
			j = last[j]
		(i, j) = (i + 1, j + 1)
		if j >= m:
			rl.append(i)
			j = last[j]
	return rl

def solve1(p, t):
	(pn, pc) = p[0]
	return sum([tn - pn + 1 for (tn, tc) in t if tn >= pn and tc == pc])

def solve2(p, t):
	return sum([t[i][0] >= p[0][0] and t[i][1] == p[0][1] and (t[i + 1][0] >= p[1][0]) and (t[i + 1][1] == p[1][1]) for i in range(len(t) - 1)])

def compact(s):
	t = [[int(s[0][:-2]), s[0][-1]]]
	for i in range(1, len(s)):
		n = int(s[i][:-2])
		c = s[i][-1]
		if c == t[-1][-1]:
			t[-1][0] += n
		else:
			t.append([n, c])
	return t

def f(p, t):
	(tt, pp) = (compact(t), compact(p))
	if len(pp) == 1:
		return solve1(pp, tt)
	if len(pp) == 2:
		return solve2(pp, tt)
	last = cal_last(pp[1:-1])
	ml = kmp(pp[1:-1], tt, last)
	x = len(pp) - 2
	n = len(tt)
	return sum([i - x > 0 and tt[i - x - 1][0] >= pp[0][0] and (tt[i - x - 1][1] == pp[0][1]) and (i < n) and (tt[i][0] >= pp[-1][0]) and (tt[i][1] == pp[-1][1]) for i in ml])
_ = input()
t = input().split()
p = input().split()
print(f(p, t))