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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define BCK(i, n) for(int i = n-1; i >= 0; i--)
#define FWD(i, a, b) for(int i = a; i < b; i++)
#define pb push_back
#define st first
#define nd second
#define vi vector<int>
#define vll vector<long long>
#define pi pair<int, int>
#define pll pair<long long, long long>
#define vpi vector<pi>
#define vpll vector<pll>
#define mp(a, b) make_pair(a,b)
using namespace std;
typedef long long LL;
typedef double ld;
const LL p = 1000000007;


int main() {
  std::ios::sync_with_stdio(false);
  int n, m, t, k;
  cin >> n >> k;
  vector<int> marks;
  vector<int> V(n+1);
  V[0] = 0;
  vector<int> new_marks_pos;
  for (int i=0; i<n; i++) {
    cin >> m;
    marks.push_back(m);
    V[i+1] = 0;
  }

  vector<int> diff_marks(n+1);
  int cnt_diff_marks = 0;
  std::fill(diff_marks.begin(), diff_marks.end(), 0);
  for (int i=0; i<n; i++) {
    if (diff_marks[marks[i]] == 0) {
      diff_marks[marks[i]] = 1;
      cnt_diff_marks++;
    }
  }

  if (cnt_diff_marks >= k) {
    std::vector<int> D;
    for (int i=0; i<k; i++) {
      if (V[marks[i]] == 0) {
        V[marks[i]] = 1;
      } else {
        D.push_back(i);
      }
    }

    int cnt_duplicats = D.size();

    LL res = 0;
    for (int i=0; i<cnt_duplicats; i++) {
      res += (k - cnt_duplicats + i - D[i]);
    }

    int cnt_new_marks = 0;
    for(int i=k; i<n; i++) {
      if (cnt_new_marks == D.size()) {
        break;
      }

      if (V[marks[i]] == 0) {
        new_marks_pos.push_back(i);
        cnt_new_marks++;
      }
    }

    int first_dupl_pos = k - cnt_duplicats; 
    for (int i=0; i < D.size(); i++) {
      // cout << new_marks_pos[i] << " " << D[i] << endl;
      res += (new_marks_pos[i] - first_dupl_pos);
      first_dupl_pos++;
    }
    cout << res << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}