#include<bits/stdc++.h>
using namespace std;
 
#define int  long long
#define sz(x) (int)x.size()
int lcm(int A,int B){    return A * B / (__gcd(A, B));}

const int mod=1e9+7;
const int mxN=2e5;

//=======================

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; pri(x); cerr << endl;
#else
#define debug(x);
#endif 


typedef unsigned long long ull;
typedef long double lld;

void pri(int t) {cerr << t;}
void pri(string t) {cerr << t;}
void pri(char t) {cerr << t;}
void pri(lld t) {cerr << t;}
void pri(double t) {cerr << t;}
void pri(ull t) {cerr << t;}

 
template <class T, class V> void pri(pair <T, V> p);
template <class T> void pri(vector <T> v);
template <class T> void pri(set <T> v);
template <class T, class V> void pri(map <T, V> v);
template <class T> void pri(multiset <T> v);
template <class T, class V> void pri(pair <T, V> p) {cerr << "{"; pri(p.ff); cerr << ","; pri(p.ss); cerr << "}";}
template <class T> void pri(vector <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T> void pri(set <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T> void pri(multiset <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T, class V> void pri(map <T, V> v) {cerr << "[ "; for (auto i : v) {pri(i); cerr << " ";} cerr << "]";}

//=======================

 

signed main() {

ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("1.txt", "w", stderr);
#endif 

int n;
cin >> n;

string s;
cin >> s;

if(s[0] != s[n-1]){
  cout << 1 << '\n';
}
else{
bool ok=false;

for(int i = 1; i <n-1; i++){
  if(s[i] != s[n-1] and s[i+1]!=s[n-1]){
    ok=true;
    break;
  }
} 
if(ok){
  cout << 2 << "\n";
}
else{
  cout << -1 << '\n';
}
}
}
#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std; 

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef vector<bool> VB;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) LL((a).size())

#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ROF(i,a,b) for(LL i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL LARGE_INT = 1e9+100;
const LL INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const LL MOD = 1e9+7;

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

LL modpow(LL a, LL n) {
    LL res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

void Main()
{
    LL n,m;cin>>n>>m;

    VL a(n);
    REP(i,n){
        cin>>a[i];
    }

    LL result = n;
    VL c(n);

    REP(i,m){
        c[a[i]]++;
    }

    LL small = n;

    REP(i,n){
        if(c[i] == 0){
            small = i;
            result = i;
            break;
        }
    }

    FOR(i,m,n){
        LL plus = a[i];
        LL minus = a[i-m];
        c[plus]++;
        c[minus]--;
        if(c[minus] == 0){
            result = min(result,minus);
        }
    }

    cout<<result<<endl;
    
	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
    return 0;
}