#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e7 + 7, MOD = 1e9 + 7, inf = 1e9, N = 3e5 + 7;
long long n, m, ans, c[maxN], a[maxN], dif, pointer = 1, k;
int main() {
  cin >> n >> k;
  for (long long i = 1; i <= n; i++)
    cin >> a[i], c[a[i]]++, dif += (c[a[i]] == 1);
  for (long long i = 1; i <= 100 * 100 * 100; i++) {
    if (dif == 1) return cout << i - 1 << "\n", 0;
    c[a[pointer]]--;
    dif -= (c[a[pointer]] == 0);
    a[n + i] = a[pointer + k - 1];
    c[a[n + i]]++;
    dif += (c[a[n + i]] == 1);
    pointer++;
  }
  if (dif == 1) return cout << 999999 << "\n", 0;
  return cout << "-1\n", 0;
}
