1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int vis[500005];
int main() {
  int n, k, cnt = 0;
  long long ans = 0;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (!vis[x]) {
      vis[x] = 1;
      cnt++;
      ans += i - cnt;
    }
  }
  if (cnt < k)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}