#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n * 3], i, j, flag = 0, p, q, pop;
  for (i = 0; i < n; i++) cin >> a[i];
  pop = a[k - 1];
  for (p = k; p <= n - 1; p++) {
    if (a[p] != pop) {
      flag = 1;
      break;
    }
  }
  if (flag == 1) {
    cout << "-1";
    return 0;
  }
  i = 0;
  while (i <= n) {
    flag = 0;
    a[n + i] = a[i + k - 1];
    pop = a[i];
    for (p = i + 1; p <= i + n; p++) {
      if (a[p] != pop) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      cout << i;
      return 0;
    }
    i++;
  }
  cout << "-1";
}
