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
#include <iostream>
#include <vector>
//#include <bits/stdc++.h>
using namespace std;

#define FORE(x, b, e) for(int x = b; x <= (e); ++x)
#define FORL(x, b, e) for(int x = b; x <  (e); ++x)

#define P(x) cout <<#x <<'=' <<(x) <<" ";
#define PP cout <<endl;
#define Pv(x,a,b) cout << #x<<'=';for(int i=a;i<=b;i++)cout<<' '<<x[i];PP
#define Pvv(x,y,a,b) cout <<#x <<#y <<'=';FORE(i,a,b)cout<<' '<<x[y[i]];PP

/*
typedef          long long  INT;
typedef unsigned long long UINT;
typedef vi vector<int>;
#define MAX 1009
UINT MOD = 1000000007ULL;
*/	

#define MAX 500000
int T[MAX+1];
	
int main()
{
	std::ios_base::sync_with_stdio(false);  cin.tie(NULL);
	
	int n,k,v;	
	int K=0,R=0;
	cin >>n >>k;
	FORE(i,1,n){
		cin >>v;
		if(!T[v]){
			T[v]=1;
			K++;
			R+=i-K;
			if(K==k) break;
		}
	}
	
	cout << ((K<k)?-1:R) <<endl;
	return 0;
}