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
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < int(n); ++i)
#define FO(i, a, b) for (int i = (a); i < int(b); ++i)
#define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((b) < (a) ? (a) : (b))

#define REMIN(a, b) ((a) = min(a, b))
#define REMAX(a, b) ((a) = max(a, b))

#define ALL(c) (c).begin(), (c).end()

#define SQR(x) ((x) * (x))

//

int main() {
  ios_base::sync_with_stdio(0);

  int n, k;

  cin >> n >> k;

  vector<bool> found(n + 10);

  long long xSum = 0;
  int foundNum = 0;

  FOR(i, n) {
    int val;
    cin >> val;

    if (found[val]) continue;

    found[val] = true;
    xSum += i;
    foundNum++;

    if (foundNum == k) break;
  }

  if (foundNum < k)
    cout << -1 << endl;
  else {
    cout << xSum - 1LL * k * (k - 1) / 2 << endl;
  }

  return 0;
}