409115: GYM103439 G Replace Sort

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. Replace Sorttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Consider an array $$$A$$$ and a set $$$B$$$ of integers such that all numbers in $$$A$$$ and $$$B$$$ are distinct. Your task is to turn $$$A$$$ into a sorted array. To do this you can take any number from $$$B$$$ and replace any element of $$$A$$$ with it. You can perform this operation any number of times, but each element of $$$B$$$ can be used at most once.

Determine the minimum number of operations needed to turn $$$A$$$ into a sorted array, or determine that it is impossible.

Input

The first line of input contains two integers $$$N$$$ and $$$M$$$ ($$$1 \le N, M \le 5 \cdot 10^5$$$) — the sizes of $$$A$$$ and $$$B$$$ respectively.

The second line contains $$$N$$$ integers $$$A_1, A_2, \ldots, A_N$$$.

The third line contains $$$M$$$ integers $$$B_1, B_2, \ldots, B_M$$$.

All the $$$(N + M)$$$ elements are distinct, positive and do not exceed $$$10^9$$$.

Output

If it is impossible to turn $$$A$$$ into a sorted array, print $$$-1$$$. Otherwise, print the minimum number of operations needed.

ExamplesInput
4 1
2 6 13 10
5
Output
-1
Input
4 2
2 6 13 10
5 4
Output
2
Input
4 3
2 6 13 10
5 4 19
Output
1
Note

In all three examples, the issue is that $$$13 > 10$$$, so we have to change at least one of them.

In the first one, we can decrease $$$13$$$ by replacing it with $$$5$$$, but it breaks the other side, so there is no solution.

In the second one, we also have $$$4$$$, which we can use to fix the broken side. It is impossible to do with less than $$$2$$$ operations.

In the third example we can finally increase the last element, thus fixing $$$A$$$ in 1 operation.

加入题单

上一题 下一题 算法标签: