Under construction

Still being built. Things may change

Easily Broadcastable Tensors

Interview

Time limit1sMemory limit2048 MB

Summary
Find the fewest 1s to insert into two size lists so that, aligned from the right, every overlapping pair is equal or contains a 1.
Level

Medium5 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

Little I is learning to use PyTorch, a very popular Python library for machine learning training.

Little I noticed that PyTorch has a mechanism for tensor operations called 'broadcasting'. You can think of tensors as multidimensional arrays. For a kk-dimensional tensor AA, a sequence of length kk, written (a1,a2,…,ak)(a_1, a_2, \ldots, a_k), gives the lengths of its dimensions. So AA is a tensor of size a1×a2×…×aka_1 \times a_2 \times \ldots \times a_k.

For two tensors AA and BB, with dimensions (a1,a2,…,am)(a_1, a_2, \ldots, a_m) and (b1,b2,…,bn)(b_1, b_2, \ldots, b_n), AA and BB are easily broadcastable if and only if the following holds: for every integer 0≤i≤min⁡(n,m)−10 \le i \le \min(n, m) - 1, either am−i=bn−ia_{m-i} = b_{n-i}, or at least one of am−ia_{m-i} and bn−ib_{n-i} is 11.

Little I has two tensors with dimensions (p1,p2,…,pm)(p_1, p_2, \ldots, p_m) and (q1,q2,…,qn)(q_1, q_2, \ldots, q_n), and they may not be easily broadcastable.

Little I can perform operations (or none) to make them easily broadcastable. Each operation chooses pp or qq and inserts a 11 at any position in the chosen sequence.

Find the minimum number of operations required to make the two tensors easily broadcastable.

Input

The first line contains two integers mm and nn (1≤m,n≤20001 \le m, n \le 2000), the dimensions of the two tensors.

The second line contains mm integers p1,p2,…,pmp_1, p_2, \ldots, p_m (1≤pi≤20001 \le p_i \le 2000), the length of each dimension of the first tensor.

The third line contains nn integers q1,q2,…,qnq_1, q_2, \ldots, q_n (1≤qi≤20001 \le q_i \le 2000), the length of each dimension of the second tensor.

Output

Print a single integer: the minimum number of insertions of 11 required to make the two tensors easily broadcastable.

Hint

In the sample, inserting a 11 before the second position of sequence qq gives 4,1,24, 1, 2, which makes the two tensors easily broadcastable.

Examples1

  1. Example 1

    Input
    4 2
    2 1 3 2
    4 2
    
    Expected output
    1