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
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
# pragma GCC diagnostic warning "-Wall"
# pragma GCC diagnostic warning "-Wextra"
# pragma GCC diagnostic warning "-Wformat=2"
# pragma GCC diagnostic warning "-Wnull-dereference"
# pragma GCC diagnostic warning "-Wduplicated-branches"
# pragma GCC diagnostic warning "-Wduplicated-cond"
# pragma GCC diagnostic warning "-Wfloat-equal"
# pragma GCC diagnostic warning "-Wshadow"
# pragma GCC diagnostic warning "-Wconversion"
# pragma GCC diagnostic warning "-Wlogical-op"
# pragma GCC diagnostic warning "-Wvector-operation-performance"
# pragma GCC diagnostic warning "-Wdisabled-optimization"
# pragma GCC diagnostic warning "-Wunsafe-loop-optimizations"
# pragma GCC diagnostic warning "-Wdouble-promotion"
#endif
#pragma GCC target "arch=ivybridge", "tune=ivybridge"
#if defined(ONLINE_JUDGE) || !defined(__OPTIMIZE__)
# pragma GCC optimize "Ofast", "inline", "unroll-loops", "ipa-pta", "no-rtti", \
  "no-exceptions", "nothrow-opt", "strict-enums", "stdarg-opt", "tracer"
# pragma GCC target "inline-all-stringops"
#endif
#define rangeof(c) (c).begin(), (c).end()

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;
  vector<bool> was_seen(n + 1, false);
  int seenc = 0;
  ll result = (ll) -k * (k - 1) / 2;
  for(int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if(seenc < k) {
      if(was_seen[num]) continue;
      was_seen[num] = true;
      seenc++;
      result += i;
    }
  }
  
  cout << (seenc < k ? -1 : result) << "\n";
}