Under construction

Still being built. Things may change

Replace Sort

Interview

Time limit3sMemory limit1024 MB

Summary
Given an array A and a set B of distinct values, find the fewest elements of A to replace with values from B so that A becomes sorted, or report -1.
Level

Medium6 of 10

Topics
Greedy, Sorting, Binary search, Array
Solved
No attempts yet

Problem

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

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

Input

The first line of input contains two integers NN and MM (1≤N,M≤5⋅1051 \le N, M \le 5 \cdot 10^5), the sizes of AA and BB.

The second line contains NN integers A1,A2,…,ANA_1, A_2, \ldots, A_N.

The third line contains MM integers B1,B2,…,BMB_1, B_2, \ldots, B_M.

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

Output

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

Hint

In all three examples, the issue is that 13>1013 > 10, so at least one of them must be changed.

In the first one, we can decrease 1313 by replacing it with 55, but that breaks the other side, so there is no solution.

In the second one, 44 can fix the broken side. It is impossible to do it with fewer than 22 operations.

In the third example, we can increase the last element, which fixes AA in 1 operation.

Examples3

  1. Example 1

    Input
    4 1
    2 6 13 10
    5
    
    Expected output
    -1
    
  2. Example 2

    Input
    4 2
    2 6 13 10
    5 4
    
    Expected output
    2
    
  3. Example 3

    Input
    4 3
    2 6 13 10
    5 4 19
    
    Expected output
    1