#K85017. Minimum Swaps to Sort

    ID: 36548 Type: Default 1000ms 256MiB

Minimum Swaps to Sort

Minimum Swaps to Sort

You are given a list of unique tokens represented by integers. Your task is to determine the minimum number of swaps required to sort the tokens in ascending order.

To solve the problem, you can decompose the permutation into cycles. For any cycle of length \(k\), the minimum number of swaps required to fix the cycle is \(k - 1\). Therefore, the answer is the sum of \(\text{cycle_size} - 1\) over all cycles.

inputFormat

The input is read from standard input (stdin). The first line contains an integer \(n\), which denotes the number of tokens. The second line contains \(n\) space-separated integers representing the tokens.

outputFormat

Output the minimum number of swaps required to sort the tokens in ascending order. The answer should be printed to standard output (stdout) in a single line.

## sample
5
2 3 4 1 5
3

</p>