cswork / OAOpen practice workspace

JPMorgan ChaseOA #33

Minimum Swaps

Estimated MediumJudge-ready

Topics (AI-labelled)In-place ArraysUnion-Find

The potentials are distinct, and each operation may swap any two positions. Find the minimum number of operations to make the array descending. Being distinct with values in 1..n means the input is a permutation of 1..n.

Input

The first line contains n, and the second line contains the permutation; full original bounds 1≤n≤200000, 1≤a[i]≤n, all distinct.

Output

Output a single integer answer.

Example 1

Input:

4
3 4 1 2

Output:

2

Example 2

Input:

1
1

Output:

0

Example 3

Input:

3
1 2 3

Output:

1

Example notes

The outputs of the three public examples are, in order: 2; 0; 1. These answers have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…