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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int,int>;
using vpii = vector<pii>;
using graph = vector<vi>;

#define FOR(name__, upper__) for (int name__ = 0; name__ < (upper__); ++name__)
#define all(x) begin(x), end(x)
#define mp make_pair
#define mt make_tuple

const int inf = 1e9;

void go() {
    int n, k; cin >> n >> k;
    vi A(n, inf); 
    
    FOR(i, n) {
        int a; cin >> a;
        A[a - 1] = min(A[a - 1], i);
    }
    sort(all(A));
    
    ll cost = 0;
    bool possible = true;
    FOR(i, k) {
        if (A[i] == inf) possible = false;
        else cost += A[i] - i;
    }
    cout << (possible? cost : -1) << '\n';
}

int main() {
	ios::sync_with_stdio(false); 
	cin.tie(0);
    
    go();

	return 0;
}