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
#include <iostream>
#include <set>
#include <vector>

using namespace std;
#undef _HOME_
#ifdef _HOME_
    #define DEBUG(x) x
#else
    #define DEBUG(x)
#endif
#define REP(x,n) for(int x=0;x<(n);++x)
#define FOREACH(x,n) for(__typeof(n.begin()) x = (n).begin(); x != (n).end(); ++x)
#define RESULT(x) {cout<<(x);return 0;}

const int MAX_N = 500005;

int n,k,tabx;
bool present[MAX_N];

int main() {
	ios_base::sync_with_stdio(0);
	cin>>n>>k;
	int toMove = 0;
	long long result = 0;
	REP(x,n) {
		cin>>tabx;
		if (!present[tabx]) {
			DEBUG(cerr<<"new: " << tabx << " / " << x << endl);
			present[tabx] = true;
			result += toMove;
			if (!(--k)) {
				RESULT(result);
			}
		}
		else {
			DEBUG(cerr<<"to move: " << tabx << " / " << x << endl);
			++toMove;
		}
	}
	RESULT(-1);
}