P9045 [PA 2021] Oranżada

Description

There is a row of $n$ bottles of orange juice, where the brand of the $i$-th bottle is $a_i$. You may spend a cost of $1$ unit to swap two adjacent bottles of orange juice. Find the minimum cost to make the leftmost $k$ bottles have pairwise distinct brands.

Input Format

The first line contains two integers $n, k$. The second line contains $n$ integers $a_1, a_2, \cdots, a_n$.

Output Format

Output one integer in one line: if a solution exists, output the minimum cost; otherwise, output $-1$.

Explanation/Hint

#### Sample #1 Explanation The optimal plan is to first swap the bottles at positions $3$ and $4$, then swap the bottles at positions $4$ and $5$, then swap the bottles at positions $2$ and $3$, and finally swap the bottles at positions $3$ and $4$, for a total of $4$ operations. #### Sample #2 Explanation Clearly, there is no solution. #### Constraints For $100\%$ of the testdata, $1 \leq k, a_i \leq n \leq 5 \times 10^5$. Translated by ChatGPT 5