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
41
42
43
44
45
46
47
48
49
50
51
52
53
//==============
// Zadanie: Oranżada (Dzień 1 PA 2021)
// URL: https://sio2.mimuw.edu.pl/c/pa-2021-1/p/ora/
// Autor: Piotr Wieczorek
//==============
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPE(i, n) for (int (i) = 0; (i) <= (n); (i)++)
#define FOR(i, z, n) for (int (i) = (z); (i) < (n); (i)++)
#define FORE(i, z, n) for (int (i) = (z); (i) <= (n); (i)++)

typedef long long ll;

//  ----------
//  SOLUTION
//  ----------
#define MAX_N 500000

// ix
vector<int> arr;
int n, k;

void init() {
	bitset<MAX_N+1> out;
	scanf("%d%d", &n, &k);
	REP(i, n) {
		int a;
		scanf("%d", &a);
		if (!out[a]){
			out[a] = 1;
			arr.pb(i);
		}
	}
}

int main() {
	init();
	if (arr.size() < k) {
		printf("-1\n");
		return 0;
	}
	ll sum = 0;
	int pos = 1;
	FOR(i, 1, k) {
		sum += arr[i] - pos;
		pos++;
	}
	printf("%lld\n", sum);
}