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
#include <iostream>
#include <algorithm>

int n, k;
int c;
long long good = 0;
long long res = 0;
bool used[500001];

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin >> n >> k;
	for( int i = 0; (i < n) && (good < k); i++ )
	{
		std::cin >> c;
		if( !used[c] )
		{
			used[c] = true;
			res += i - good;
			good++;
		}
	}
	if( good == k ) std::cout << res;// << '\n';
	else std::cout << -1;//<< '\n';
	return 0;
}