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>
#define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug(x) cerr<<#x<<" "<<x<<endl
#define ll long long 
#define st first
#define nd second
using namespace std;

int n, t, k, a[500005], wys[500005], x, y;
ll res;
vector <int> lewo, prawo;
int main()
{
	qio;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		wys[a[i]]++;
		if (i <= k) {
			if (wys[a[i]] > 1) lewo.push_back(i);
		}
		else {
			if (wys[a[i]] == 1) prawo.push_back(i);
		}
	}

	if (prawo.size() < lewo.size()) {
		cout << -1 << endl;
		return 0;
	}

	x = k;
	for (int i = lewo.size() - 1; i >= 0; i--) {
		res += x - lewo[i];
		x--;
	}

	y = k;
	for (int i = 0; i < lewo.size(); i++) {
		res += prawo[i] - y;
		y--;
	}
	cout << res << endl;
}