#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 4020000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
    vector<T> dat;
    ll sz;
    //all 1-indexed
    BIT(ll sz) : sz(sz){
        dat.assign(++sz, 0);
    }

    T sum(ll k){
        T ret = 0;
        for(++k; k > 0; k -= k & -k) ret += dat[k];
        return (ret);
    }

    void add(ll k, T x){
        for(++k; k < dat.size(); k += k & -k) dat[k] += x;
    }
    
    ll get(T k){
        if(k <= 0) return 0; 
        ll ret = 0;
        int n = 1; while(n < sz) n *= 2;
        for(int i=n/2; i>0; i/=2){
            if(ret+i < sz && dat[ret+i] < k){
                k -= dat[ret+i];
                ret += i;
            }
        }
        return ret;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    vl a(n); rep(i,n) cin >> a[i];
    vl cnt(n,0);
    BIT<int> bit(n);
    rep(i,m){
        cnt[a[i]]++;
        if(cnt[a[i]] == 1){
            bit.add(a[i],1);
        }
    }
    int mn = n;
    rep(i,n-m+1){
        int ok = m, ng = -1;
        while(ok-ng > 1){
            int mid = (ok+ng) / 2;
            if(bit.sum(mid) < mid+1) ok = mid;
            else ng = mid;
        }
        chmin(mn,ok);
        cnt[a[i]]--;
        if(cnt[a[i]] == 0){
            bit.add(a[i],-1);
        }
        if(i<n-m){
            cnt[a[i+m]]++;
            if(cnt[a[i+m]] == 1){
                bit.add(a[i+m],1);
            }
        }
    }
    cout << mn << "\n";
}
#include <bits/stdc++.h>
using namespace std;
int arr[1500002];
int krr[1500002];
int tree[4*1500002];
void update(int node,int b,int e,int i,int val)
{
    if(b==e&&b==i){
        tree[node]=val;
        return;
    }
    int mid=(b+e)>>1;
    int left=node*2;
    int right=left+1;
    if(i<=mid)update(left,b,mid,i,val);
    else update(right,mid+1,e,i,val);
    tree[node]=tree[left]+tree[right];
}
int query(int node,int b,int e)
{
    if(b==e){
        return b;
    }
    int mid=(b+e)>>1;
    int left=node<<1;
    int right=left+1;
    if(tree[left]>0)return query(left,b,mid);
    else return query(right,mid+1,e);
}
int main()
{
    int n,m;
    map<int,int>maps;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&arr[i]);
        arr[i]++;
    }
    n+=10;
    for(int i=1;i<=n;i++)update(1,1,n,i,1);
    int ans=INT_MAX;
    for(int i=1;i<=m;i++){
        maps[arr[i]]++;
        if(maps[arr[i]]>0)update(1,1,n,arr[i],0);
    }
    ans=min(ans,query(1,1,n));
    for(int i=m+1;i<=n-10;i++){
        int l=i-m;
        int r=i;
        maps[arr[l]]--;
        if(maps[arr[l]]<=0)update(1,1,n,arr[l],1);
        maps[arr[r]]++;
        if(maps[arr[r]]>0)update(1,1,n,arr[r],0);
        ans=min(ans,query(1,1,n));
    }
    printf("%d\n",ans-1);
    return 0;
}
