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
#include <cstdio>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define INT(x) int x; scanf("%d", &x)

typedef long long LL;

bool u[500000];

int main() {
	INT(n);
	INT(k);
	LL r = 0;
	int ii = 0;
	REP(i,n) {
		INT(a);
		--a;
		if (u[a]) continue;
		u[a] = 1;
		r += i - ii++;
		if (ii == k) {
			printf("%lld\n", r);
			return 0;
		}
	}
	printf("-1\n");
}