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

#define FOR(i,a,b)    for (int i=(a)  ;i<(b) ;i++)
#define RFOR(i,a,b)   for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)      for (int i=0    ;i<(n) ;i++)
#define RREP(i,n)     for (int i=(n)-1;i>=0  ;i--)
#define EACH(i,a,b)   for (int i=(a)  ;i<=(b);i++)
#define REACH(i,a,b)  for (int i=(b)  ;i>=(a);i--)
#define ALL(a)        (a).begin(),  (a).end()
#define RALL(a)       (a).rbegin(), (a).rend()
#define YES()         printf("YES\n")
#define NO()          printf("NO\n")
#define Yes()         printf("Yes\n")
#define No()          printf("No\n")
#define in(a,x,b)     ((a) <= (x) && (x) < (b))
#define PB            push_back

const int MOD = 1e9 + 7;
const int INF = 1 << 29;
const double EPS = 1e-10;

using ll = long long;
using V = vector<int>;

template<typename T> bool inline chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<typename T> bool inline chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<typename T = int> T inline input() { T x; cin >> x; return (x); }
template<typename T> void inline print(T& x) { cout << x << '\n'; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(16);

  int N,M;
  cin >> N >> M;
  V A(N);
  REP(i,N) cin >> A[i];

  map<int,int> cnt;
  set<int> s;
  EACH(i,0,N) {
    s.insert(i);
  }

  int ans = INF;
  REP(i,M) {
   int a = A[i];
   cnt[a]++;
   s.erase(a);
  }

  chmin(ans, *s.begin());
  REP(i,N-M) {
    int aleft = A[i];
    int aright = A[i+M];
    cnt[aleft]--;
    if (cnt[aleft] == 0) s.insert(aleft);
    cnt[aright]++;
    s.erase(aright);
    chmin(ans, *s.begin());
  }

  print(ans);
}
#include <cstdio>
const int maxn = 1.5e6+5;
inline int min(int a,int b){return a<b?a:b;}
int n,m,a[maxn],cnt[maxn],mex[maxn<<2],ans=1919810;
void build(int k,int l,int r){
	if(l == r)return mex[k]=l,void();
	int mid = l+r>>1;
	build(k<<1,l,mid),build(k<<1|1,mid+1,r),mex[k] = min(mex[k<<1],mex[k<<1|1]);
}
void insert(int k,int l,int r,int p,int v){
	if(l == r){
		if(v)++cnt[l],(cnt[l]==1?mex[k]=1919810:0);
		else --cnt[l],(cnt[l]==0?mex[k]=l:0);
		return ;
	}
	int mid = l+r>>1;
	p<=mid?insert(k<<1,l,mid,p,v):insert(k<<1|1,mid+1,r,p,v);
	mex[k] = min(mex[k<<1],mex[k<<1|1]);
}
int main(){
	scanf("%d %d",&n,&m),build(1,1,n+1);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]),++a[i];
	for(int i=1;i<=m;++i)insert(1,1,n+1,a[i],1);
	ans=min(ans,mex[1]);
	for(int i=m+1;i<=n;++i)insert(1,1,n+1,a[i-m],0),insert(1,1,n+1,a[i],1),ans=min(ans,mex[1]);
	printf("%d\n",ans-1);
	return 0;
}