atcoder#ARC124D. [ARC124D] Yet Another Sorting Problem

[ARC124D] Yet Another Sorting Problem

Score : 700700 points

Problem Statement

Given is a sequence pp of length N+MN+M, which is a permutation of (1,2,N+M)(1,2 \ldots, N+M). The ii-th term of pp is pip_i.

You can do the following Operation any number of times.

Operation: Choose an integer nn between 11 and NN (inclusive), and an integer mm between 11 and MM (inclusive). Then, swap pnp_{n} and pN+mp_{N+m}.

Find the minimum number of Operations needed to sort pp in ascending order. We can prove that it is possible to sort pp in ascending order under the Constraints of this problem.

Constraints

  • All values in input are integers.
  • 1N,M1051 \leq N,M \leq 10^5
  • 1piN+M1 \leq p_i \leq N+M
  • pp is a permutation of (1,2,N+M)(1,2 \ldots, N+M).

Input

Input is given from Standard Input in the following format:

NN MM

p1p_{1} \cdots pN+Mp_{N+M}

Output

Print the minimum number of Operations needed to sort pp in ascending order.

2 3
1 4 2 5 3
3
5 7
9 7 12 6 1 11 2 10 3 8 4 5
10