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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Grzegorz Suwaj
// TEMPLATE
// */
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FE(i, a, b) for (size_t i = a; i < b; i++)
#define FI(i, a, b) for (size_t i = a; i <= b; i++)
#define RFE(i, a, b) for (int i = a; i > b; i--)
#define RFI(i, a, b) for (int i = a; i >= b; i--)
#define RANGE(c) (c).begin(), (c).end()
#define RRANGE(c) (c).rbegin(), (c).rend()
#define mp std::make_pair
#define fi first
#define se second
#define th third
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define bgn begin
#define ers erase
#define ins insert
#define putcxi putchar_unlocked
#define getcxi getchar_unlocked
#define y1 _y1
#define y0 _y0
#define esle else
#define szie size
#define siez size
#define szie_t size_t
#define isze size
#define boid void
#define itn int
#define breal break
#define bakc back

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pi = std::pair<int, int>;
using pill = std::pair<int, ll>;
using plli = std::pair<ll, int>;
using pll = std::pair<ll, ll>;
using pld = std::pair<ld, ld>;
using vi = std::vector<int>;
using vll = std::vector<ll>;
using vull = std::vector<ull>;
using vld = std::vector<ld>;
using vpi = std::vector<pi>;
using vpill = std::vector<pill>;
using vplii = std::vector<plli>;
using vpll = std::vector<pll>;
using vpld = std::vector<pld>;
using vvi = std::vector<vi>;
using vvll = std::vector<vll>;
using vvvi = std::vector<vvi>;
using vvpi = std::vector<vpi>;
using vb = std::vector<bool>;
using vvb = std::vector<vb>;
using si = std::set<int>;
using sll = std::set<ll>;
using sull = std::set<ull>;
using spi = std::set<pi>;

const int MAX_INT = std::numeric_limits<int>::max();
const int MXI = 1e9;
const ll MAX_LL = std::numeric_limits<long long>::max();
const ull MAX_ULL = std::numeric_limits<unsigned long long>::max();
const ll MXLL = 1e18;

template<typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p)
{
    //     return os << p.fi << ' ' << p.se << ' ';
    return os << p.fi << ' ' << p.se;
}

template<typename... T, template<typename...> class Container, typename std::enable_if<!std::is_same<Container<T...>, std::string>::value>::type * = nullptr>
std::ostream &operator<<(std::ostream &os, const Container<T...> &c)
{
    for (auto &&x: c)
        os << x << ' ';
    // os << '\\n';
    return os;
}

// /*
// KOD CODE PROGRAM SOURCE
// */
int n, k;
vi a;

int main(int argc, char const *argv[])
{
#ifndef DEBUG
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
#endif

    std::cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        int a_i;
        std::cin >> a_i;
        a.pb(a_i);
    }

    ll ans = 0;
    vb vis(n + 1);
    int i = 0, j = 1;
    while (i < k && j < n) {
        if (vis[a[i]]) {
            while (j < n && vis[a[j]])
                j++;
            if (j >= n) {
                std::cout << -1;
                return 0;
            }
            std::swap(a[i], a[j]);
            ans += ll((j++) - i);
        }
        vis[a[i++]] = true;
    }

    std::cout << ans;
    return 0;
}