#include <bits/stdc++.h>
using namespace std;

#define forn(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define f first
#define s second
#define rand(i, j) uniform_int_distribution<ll>((ll)(i), (ll)(j))(rng)
mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef double ld;
typedef long double lld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vii;

//Printing pairs and vectors
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
	cout << "["; for (int i = 0; i < (int)v.size(); i++) {if (i) cout << ", "; cout << v[i]; } return cout << "]";
}

//2 147 483 647 int max
//9 223 372 036 854 775 807 ll max


int main(){
	cin.tie(0)->sync_with_stdio(0);

	int n;
	string s;
	cin >> n >> s;
	if(s.at(0) != s.at(n-1)){
		cout << 1 << "\n";
	}else{
		for(int i=1; i<n-1; i++){
			if(s.at(i) != s.at(0) && s.at(i+1) != s.at(0)){
				cout << 2 << "\n";
				return 0;
			}
		}
		cout << -1 << "\n";
	}
}
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int cur = 0;
  vector<int> cnt(n+1, 0);
  for (int i = 0; i < m; ++i) {
    cnt[a[i]]++;
    while (cnt[cur]) cur++;
  }
  int ans = cur;
  int left = 0, right = m-1;
  for (int i = 0; i < n-m; ++i) {
    right++;
    cnt[a[right]]++;
    cnt[a[left]]--;
    if (cur > a[left] && cnt[a[left]] == 0) cur = a[left];
    left++;
    while (cnt[cur]) cur++;
    if (ans > cur) ans = cur;
  }
  cout << ans << endl;
  return 0;
}