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 <bits/stdc++.h>

int main()
{
	std::ios_base::sync_with_stdio(0);
	
	int n;
	int k;

	std::cin >> n >> k;

	std::vector<int> v(n, 0);
	std::unordered_map<int, int> m;
	std::vector<int> ov;
	ov.reserve(n);

	for(int i=0; i<n; i++)
	{
		std::cin >> v[i];
		if(m.count(v[i]) == 0)
		{
			m[v[i]] = i;
			ov.push_back(v[i]);
		}
	}

	if(int(ov.size()) < k)
	{
		std::cout << "-1" << "\n";
		return 0;
	}

	long long result = 0;

	for(int i=1;i<k;i++)
	{
		int dist = m[ov[i]]-i;
		result += dist;
	}

	std::cout << result << "\n";

	return 0;
}