Zigzag

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

문제

We say that a sequence of numbers x(1),x(2),,x(k)x(1), x(2), \ldots, x(k) is zigzag if no three of its consecutive elements form a nonincreasing or nondecreasing sequence. More precisely, for all i=1,2,,k2i = 1, 2, \ldots, k-2, either x(i+1)<x(i)x(i+1) < x(i) and x(i+1)<x(i+2)x(i+1) < x(i+2) or x(i+1)>x(i)x(i+1) > x(i) and x(i+1)>x(i+2)x(i+1) > x(i+2).

You are given two sequences of numbers a(1),a(2),,a(n)a(1), a(2), \ldots, a(n) and b(1),b(2),,b(m)b(1), b(2), \ldots, b(m). The problem is to compute the length of the longest common zigzag subsequence.  In other words, you are going to remove elements from the two sequences so that they are equal, and what remains from each sequence is a zigzag sequence. If the minimum number of elements you have to remove is kk, then your answer is m+nkm+n-k.

입력

The first line contains the length of the first sequence nn (1n20001\leq n \leq 2000) followed by nn integers a(1),a(2),,a(n)a(1), a(2), \ldots, a(n). The second line contains the length of the second sequence mm (1m20001 \leq m \leq 2000) followed by mm integers b(1),b(2),,b(m)b(1), b(2), \ldots, b(m). All a(i)a(i) and b(i)b(i) are from the range \[109,109]\[-10^9, 10^9].

출력

Output one line containing the length of the longest common zigzag subsequence of aa and bb.