1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
#define gc getchar
#define gcu getchar_unlocked
#define fi first
#define se second
#define pb push_back
#define mod ((ll)1e9 + 7)
typedef long long ll;
using namespace std;
//=============================================

int n, m;
ll ans;
int a[1000009];
set<int> s;

//=============================================
int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (int i = 0, temp = 0; i < n; i++)
	{
		if (s.count(a[i]))
			continue;
		s.insert(a[i]);
		m--;
		swap(a[i], a[temp]);
		ans += i - temp;
		temp++;
		if (!m)
			break;
	}
	if (!m)
		printf("%lld\n", ans);
	else
		printf("-1\n");
}
//=============================================