QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 10

#2116. Orangeade [B]

统计

Bajtabasz likes to stay at home. After all, we are in the middle of a pandemic – social distancing must be maintained. During the evenings spent in front of the computer, his favorite activity, right next to playing Byte Defence 3 and solving problems from old editions of Potyczki Algorytmiczne, is drinking orangeade. In Bajtabasz's cellar, there is a long shelf on which $n$ bottles of orangeade are arranged in a row. Each bottle is of a certain brand, which we denote by integers. The cellar is cramped and has a slippery floor, so it would be reckless to walk back and forth with bottles in his hands – one might break. For this reason, the only thing Bajtabasz can do is swap two adjacent bottles. Each such swap takes him one second. The time spent moving along the shelf is negligible.

For this evening, Bajtabasz has invited Bajtolina for a joint orangeade tasting. To brighten up this event, he would like the $k$ leftmost bottles on the shelf to be of different brands.

Bajtabasz has little time – he still has to clean his whole house – so he would like to rearrange the orangeade shelf as quickly as possible. Help him with this task!

Input

The first line of input contains two integers $n$ and $k$ ($1 \le n \le 5 \cdot 10^5$; $1 \le k \le n$), representing the number of bottles on the shelf and the number of leftmost bottles that must be of different brands to make Bajtabasz happy, respectively.

The second line of input contains a sequence of $n$ integers $a_1, a_2, a_3, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ denotes the brand of the $i$-th bottle from the left on the shelf in Bajtabasz's cellar.

Output

The output should contain a single integer representing the minimum number of seconds after which the first $k$ bottles will be pairwise distinct, or $-1$ if it is impossible to reach such a situation.

Examples

Input 1

5 3
3 3 3 1 2

Output 1

4

Input 2

3 2
1 1 1

Output 2

-1

Note

Explanation of the example: In the first sample test, a possible sequence of swaps is: $3, 3, 3, 1, 2$ – initial configuration, $3, 3, 1, 3, 2$ – swap bottles at positions 3 and 4, $3, 3, 1, 2, 3$ – swap bottles at positions 4 and 5, $3, 1, 3, 2, 3$ – swap bottles at positions 2 and 3, * $3, 1, 2, 3, 3$ – swap bottles at positions 3 and 4.

It is impossible to make the first three bottles of different brands in less than 4 seconds.

In the second sample test, all three orangeades are of the same brand, so we cannot make the first two of different brands.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.