Compressed LCS

아직 제출이 없습니다시간 제한3초메모리 제한512 MB

문제

Bobo has two integer sequences AA and BB, both in compressed form. A=c_1a_1c_2a_2c_na_nA = c\_1^{a\_1} c\_2^{a\_2} \dots c\_n^{a\_n} means that AA begins with a_1a\_1 copies of the integer c_1c\_1, followed by a_2a\_2 copies of the integer c_2c\_2, a_3a\_3 copies of the integer c_3c\_3, and so on. B=d_1b_1d_2b_2d_mb_mB = d\_1^{b\_1} d\_2^{b\_2} \dots d\_m^{b\_m} is of similar format.

Bobo would like to find the LCS (longest common subsequence) for AA and BB. Recall that sequence CC is a subsequence of AA if and only if CC can be obtained by deleting some (maybe all, maybe none) elements from AA.

입력

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains two integers nn and mm (1n,m20001 \leq n, m \leq 2000).

The ii-th of the following nn lines contains two integers c_ic\_i and a_ia\_i. And the ii-th of the last mm lines contains two integers d_id\_i and b_ib\_i.
The constraints are: 1a_i,b_i,c_i,d_i,_i=1na_i,_i=1mb_i1091 \leq a\_i, b\_i, c\_i, d\_i, \sum\limits\_{i = 1}^n a\_i, \sum\limits\_{i = 1}^m b\_i \leq 10^9, c_ic_i1c\_i \neq c\_{i - 1}, d_id_i1d\_i \neq d\_{i - 1}.

It is guaranteed that the sum of nn and the sum of mm both do not exceed 20002000.

출력

For each test case, output an integer which denotes the length of the LCS.