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
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define p_b push_back
#define m_p make_pair
#define fi first
#define se second
 
ll inf = 8000000000000000000;
ll mod = 30000;
const int maxn = 200003;

int main(){
    ios_base::sync_with_stdio(0);
    int n, k; cin>>n>>k;
    vector<int> t(n);
    for(int i = 0; i<n; i++){
        cin>>t[i];
        t[i]--;
    }

    vector<bool> b(n);
    ll res = 0, z = 0, o = -1;
    for(int i = 0; i<n; i++){
        int a = t[i];
        if(!b[a]){
            z++;
            b[a] = true;
            res += i-o-1;
            o++;
        }
        if(z==k) break;
    }

    if(z!=k) cout<<-1;
    else cout<<res;
}