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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC target("sse,sse2,sse3,mmx,abm,tune=native")
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
#define For(i,s,a) for(int i = (int)s; i < (int)a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define sz size()
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define make_unique(x) (x).erase( unique(all(x)), (x).end())
#define popcnt(x) __builtin_popcount(x)

//using namespace std::chrono;
//#define time_since duration_cast<nanoseconds>(system_clock::now().time_since_epoch())

template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
	return os << x.ff << " " << x.dd;
}

const int N = 5e5 + 1;
int c[N], ile[N];

bool verify(int n, int k) {
	int ilee = 0;
	For(i, 0, n)
		if(ile[c[i]] == 0)
			++ile[c[i]],
			++ilee;
	memset(ile, 0, N * 4);
	if(ilee >= k)
		return 0;
	return 1;
}

int32_t main() {
	int n, k;
	scanf("%d%d", &n, &k);
	For(i, 0, n)
		scanf("%d", &c[i]);
		
	if(verify(n, k)) {
		puts("-1");
		return 0;
	}
	
	lld wyn = 0;
	int ct = 0;
	For(i, 0, k) {
		++ile[c[i]];
		if(ile[c[i]] == 1)
			++ct;
	}
	
	int wsk = k;
	for(int i = k - 1; i + 1; --i) {
		if(ct >= k)
			break;
		if(ile[c[i]] == 1)
			continue;
		while(ile[c[wsk]])
			++wsk;
		wyn += wsk - i;
		--ile[c[i]];
		++ile[c[wsk]];
	}	
	
	printf("%lld\n", wyn);
}