Bobo has two integer sequences A and B, both in compressed form. A=c_1a_1c_2a_2…c_na_n means that A begins with a_1 copies of the integer c_1, followed by a_2 copies of the integer c_2, a_3 copies of the integer c_3, and so on. B=d_1b_1d_2b_2…d_mb_m is of similar format.
Bobo would like to find the LCS (longest common subsequence) for A and B. Recall that sequence C is a subsequence of A if and only if C can be obtained by deleting some (maybe all, maybe none) elements from A.
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 n and m (1≤n,m≤2000).
The i-th of the following n lines contains two integers c_i and a_i. And the i-th of the last m lines contains two integers d_i and b_i.
The constraints are: 1≤a_i,b_i,c_i,d_i,∑_i=1na_i,∑_i=1mb_i≤109, c_i=c_i−1, d_i=d_i−1.
It is guaranteed that the sum of n and the sum of m both do not exceed 2000.
For each test case, output an integer which denotes the length of the LCS.