atcoder#ABC185E. [ABC185E] Sequence Matching

[ABC185E] Sequence Matching

Score : 500500 points

Problem Statement

We have an integer sequence AA of length NN and an integer sequence BB of length MM. Takahashi will make a new sequence AA' by removing some elements (possibly zero or all) from AA and concatenating the remaining elements. Similarly, he will make another new sequence BB' by removing some elements (possibly zero or all) from BB and concatenating the remaining elements. Here, he will remove elements so that A=B|A'| = |B'|. (s|s| denotes the length of ss for a sequence ss.) Let xx be the total number of elements removed from AA and BB, and yy be the number of integers ii such that 1iA1 \le i \le |A'| and AiBi{A'}_i \neq {B'}_i. Print the minimium possible value of x+yx + y.

Constraints

  • 1N,M10001 \le N, M \le 1000
  • 1Ai,Bi1091 \le A_i, B_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

$A_1 \hspace{7pt} A_2 \hspace{7pt} A_3 \hspace{5pt} \dots \hspace{5pt} A_N$

$B_1 \hspace{7pt} B_2 \hspace{7pt} B_3 \hspace{5pt} \dots \hspace{5pt} B_M$

Output

Print the minimum possible value of x+yx + y.

4 3
1 2 1 3
1 3 1
2

If we make AA' by removing A4A_4 from AA, and BB' by removing nothing from BB, xx will be 11. Here, there is just one integer ii such that 1iA1 \le i \le |A'| and AiBi{A'}_i \neq {B'}_i: i=2i = 2, so yy will be 11, and x+yx + y will be 22, which is the minimum possible value.

4 6
1 3 2 4
1 5 2 6 4 3
3

If we remove nothing from AA and remove B4,B6B_4, B_6 from BB, we have x=2,y=1x = 2, y = 1, and x+y=3x + y = 3, which is the minimum possible value.

5 5
1 1 1 1 1
2 2 2 2 2
5

It is allowed to remove nothing from both AA and BB.