#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  map<int, int> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m[a[i]]++;
  }
  if (m[a[0]] == n) {
    cout << 0 << endl;
    return 0;
  }
  for (int j = 0; j < n; ++j) {
    int x = a[j + k - 1];
    m[x]++;
    m[a[j]]--;
    a.push_back(x);
    if (m[x] == n) {
      cout << j + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
