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
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
using namespace std;

template<class T, class Y>
ostream& operator<<(ostream& out, const pair<T,Y>& p)
{out << p.first << ": " << p.second; return out;}

template<class T>
ostream& __print(ostream& out, string st, string en, string sep, T con)
{out << st;typename T::size_type i=0; for (auto it=con.begin(); it!=con.end(); ++it, i++)
{out << *it << (i<con.size()-1 ? sep : "");}
out << en; return out;}

#define register_print1(a, b, c, d) template <class T> \
	ostream& operator<<(ostream& out, const a<T>& con)   \
	{return __print(out, b, c, d, con);}
#define register_print2(a, b, c, d) template <class T, class Y> \
	ostream& operator<<(ostream& out, const a<T, Y>& con)         \
	{return __print(out, b, c, d, con);}

register_print1(vector, "[", "]", ", ")
register_print1(set, "{", "}", ", ")
register_print1(unordered_set, "{", "}", ", ")
register_print2(map, "{", "}", ", ")
register_print2(unordered_map, "{", "}", ", ")

void solve() {
	int n, k;
	cin >> n >> k;

	vector<int> nums(n);
	vector<int> cnt(n);
	for (int i=0; i<n; i++) {
		cin >> nums[i];
		nums[i]--;
	}

	// cout << nums << "\n" << cnt << "\n";
	unordered_set<int> state;
	vector<int> indexes;

	int stop = -1;
	for (int i=0; i<n; i++) {
		int size_p = state.size();
		state.insert(nums[i]);
		cnt[i] = state.size();

		if (cnt[i] != size_p) {
			indexes.push_back(i);
		}

		if (cnt[i] == k) {
			stop = i;
			break;
		}
	}

	if (stop == -1) {
		cout << "-1\n";
		return;
	}

	int mov = 0;
	int off = 0;
	for (int x: indexes) {
		mov += (x - off);
		off += 1;
	}

	//cout << nums << "\n" << cnt << "\n" << indexes << "\n" << mov << "\n" ;
	cout << mov << "\n";
/*

3 3 3 1 2 - 0

3 3 3 1 2 - 0
3 3 1 3 2 - 1
3 1 3 3 2 - 2

3 1 3 3 2 - 2
3 1 3 2 3 - 3
3 1 2 3 3 - 4

*/



}

int main() {solve();}