Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/saralash-almashtirishlar-soni

Swaps to sort

Medium 1000 ms 128 MB 56%
Share

$n$ distinct integers are given. In one operation any two elements can be swapped. What is the smallest number of operations needed to sort the array in increasing order?

Arrows from the array elements to their places after sorting; the arrows form cycles

Input

The first line contains a positive integer $n$. The second line contains $n$ distinct integers $a_{i}$.

Output

One integer — the smallest number of operations.

Note

$1 \le n \le 2 \cdot 10^{5}$
$-10^{9} \le a_{i} \le 10^{9}$, all $a_{i}$ are distinct

Examples

Input #1
5
4 3 1 5 2
Output #1
4
Input #2
1
10
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.