#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int MOD = 1000000007;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

signed main(){
  int N, M;
  cin >> N >> M;

  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<int> fg(N+1, -1);
  rep(i, M) fg[A[i]] = i;

  //現時点での最小
  priority_queue<int, vector<int>, greater<int>> pq;
  rep(i, N+1){
    if( fg[i] == -1 ) pq.push(i);
  }

  int ans = pq.top();
  for( int i = M; i < N; i++ ){
    fg[A[i]] = i;
    if( pq.top() == A[i] ) pq.pop();
    if( fg[A[i-M]] == i-M ) pq.push(A[i-M]);
    ans = min(ans, pq.top());
  }
  cout << ans << endl;

  
}

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

const int L = 1.5 * 1e6 + 2;
int n, m, a[L], pos[L], dis[L];
bool vis[L];

int main() {
	scanf("%d %d", &n, &m);
	
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		a[i]++, vis[a[i]] = true;
		dis[a[i]] = max(dis[a[i]], i-pos[a[i]]);
		pos[a[i]] = i;
	}
	
	for (int i = 1; i < L; i++) {
		dis[i] = max(dis[i], n-pos[a[i]]+1);
	
		if (dis[i] > m || !vis[i]) {
			printf("%d", i-1);
			return 0;
		}
	}
	
	printf("%d", L-1);
	
	return 0;
} 