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


int main(){
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(nullptr);
    int k, n, b;
    int zamiany{0};
    std::unordered_set<int> set;
    std::vector<int> shelf;
    std::cin >> n >> k;
    for(int i = 0; i < n; i++){
        std::cin >> b;
        shelf.push_back(b);
    }
    int i{0};
    while(i < n && set.find(shelf[i]) == set.end() && set.size() < k){
        set.insert(shelf[i]);
        i++;
    }
    if(set.size() == k){
        std::cout << 0 << "\n";
        return 0;
    }
    //i == pierwsze powtórzenie
    i++;

    while(i < n && set.size() < k){
       
        if(set.find(shelf[i]) == set.end()){
            zamiany += (int) (i - set.size());
            set.insert(shelf[i]);
        }
        i++;
    }
    if(set.size() == k){
        std::cout << zamiany <<"\n";
    }
    else{
        std::cout << -1 << "\n";
    }
    return 0;
}