#P12176. Book Rearrangement

    ID: 14279 Type: Default 1000ms 256MiB

Book Rearrangement

Book Rearrangement

In a remote library, there is a bookshelf with $n$ books, each labeled with a unique number from $1$ to $n$. The books are supposed to be arranged in increasing order from left to right: book $1$ on the left-end, book $2$ immediately following, and so on, until book $n$ on the right-end. However, due to a hectic day of borrowing and returning, the order of the books has been disturbed. The current arrangement is represented by a permutation $a = (a_1, a_2, \ldots, a_n)$, where $a_i$ indicates the book number at position $i$.

Your task is to determine the minimum number of swap operations required to restore the order to $(1, 2, \ldots, n)$. In each operation, you can select any two books and swap their positions. Note that if a cycle in the permutation contains $k$ elements, it will take exactly $k-1$ swaps to fix that cycle.

inputFormat

The first line contains an integer $n$ ($1 \leq n \leq 10^5$) representing the number of books. The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$, which denote the current arrangement of the books.

outputFormat

Output a single integer indicating the minimum number of swap operations required to sort the books into increasing order.

sample

3
1 2 3
0