1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e9 + 99;

int main(){
	ll n, k;
	cin >> n >> k;
	vector<ll> t(n+9, INF);
	vector<bool> seen(n+9, 0);
	int cnt = 0;
	for(ll i=0,a; i<n; i++){
		cin >> a;
		if(seen[a] == 0){
			seen[a] = 1;
			t[cnt] = i;
			cnt++;
		}
	}
	ll wynik = 0;
	for(ll i=0; i<k; i++)
		wynik = (wynik == -1 || t[i] == INF) ? -1 : (wynik + t[i] - i);
	cout << wynik << "\n";
}