cswork / OAOpen practice workspace

AmazonOA #298

Minimum Insertions to Make List a Subsequence (Amazon Germany :)

Estimated HardJudge-ready

Topics (AI-labelled)SubsequencesBinary SearchHashing

initialList may contain duplicate integers, and the integers in finalList are distinct. In each step you may insert one positive integer at any position in initialList; you cannot delete or reorder existing elements. Find the minimum number of insertions so that finalList becomes a subsequence of the final initialList. Zeros and negative integers in the target cannot be obtained by insertion and must match existing elements; if it is impossible, output −1 as this site's protocol specifies.

Input

The first line contains m n, the second line contains the m integers of initialList, and the third line contains the n integers of finalList. On this site 1≤m,n≤200000, −10^9≤every element≤10^9, and finalList is distinct. The rule that only positive numbers may be inserted is not misread as requiring the original array to be all positive. The original statement does not define the output when there is no solution; this site does not exclude unsolvable inputs and explicitly encodes them as −1.

Output

Output a single integer, the answer.

Example 1

Input:

4 4
1 2 3 -1
-1 1 2 3

Output:

3

Example 2

Input:

2 3
1 3
1 2 3

Output:

1

Example 3

Input:

2 2
-1 0
0 -1

Output:

-1

Example notes

The answers are 3, 1 and −1. In the first example the final −1 must be kept, and then 1, 2 and 3 are inserted; in the third example the order of the anchors is impossible. One place in the original statement reverses the subsequence direction; based on Returns and the complete worked example, this site specifies that final is a subsequence of initial, and openly adds −1 for the no-solution case.

Editorial

Loading solution…