#include <iostream>
#include <vector>
using namespace std;
void chmin(int& a, int b){ if(a > b) a = b; }


int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<int> idx(N, -1);
    int ans = N;
    for(int i = 0; i < N; i++){
        int A;
        cin >> A;
        if(i - idx[A] > M) chmin(ans, A);
        idx[A] = i;
    }
    for(int i = 0; i < ans; i++) if(N - idx[i] > M){
        cout << i << endl;
        return 0;
    }
    cout << ans << endl;
}
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <bitset>
using namespace std;


const int INF  = 1000000001;
const int MAX_N = 2000000 + 10;
int A[MAX_N];
int s[MAX_N]; //set

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> A[i];

  int mex;
  for (int i = 0; i < M; i++) s[ A[i] ]++;
  for (int i = 0; i < N; i++) {
    if(s[i] == 0) {
      mex = i;
      break;
    }
  }
  
  int ans = mex;
  for (int i = 1; i <= N - M; i++) {
    int mex_prev = mex;
    s[A[i-1]]--;
    if(s[A[i-1]] == 0) mex = min(A[i-1], mex);
    
    s[A[i+M-1]]++;
    if(s[A[i+M-1]] == 1) {
      if(A[i+M-1] > mex) mex = mex;
      else if (A[i+M-1] == mex) mex = mex_prev;
      else mex = mex;
    }
    ans = min(ans, mex);
  }

  cout << ans << endl;
  return 0;
}




