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
#include <iostream>

using namespace std;

constexpr int MAXN = 5 * 1e5;

bool was_number[MAXN + 1];

int main()
{
    int n, k;
    cin >> n >> k;

    int steps = 0;
    int prev_repeat = 0;
	int diff_nums = 0;
    for (int i = 0; i < n && diff_nums < k; ++i)
    {
        int x;
        cin >> x;

        if (!was_number[x])
        {
			++diff_nums;
            steps += prev_repeat;
            was_number[x] = true;
        }
        else
        {
            ++prev_repeat;
        }
    }
	
	if (diff_nums < k)
		cout << -1 << "\n";
	else
		cout << steps << "\n";

    return 0;
}