#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,srt,end) for(int i = (srt); i < (int)(end); i++)
//cout << std::fixed << std::setprecision(15) << y << endl;

// For debug
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }

// mex : https://rsk0315.hatenablog.com/entry/2020/10/11/125049
struct nekoset {
private:
    std::set<std::pair<int, int>> s;
    std::map<int, int> nums;

public:
    nekoset() {
        s.emplace(INT_MIN, INT_MIN);
        s.emplace(INT_MAX, INT_MAX);
    }
 
    bool contains(int x) const {
        auto it = std::prev(s.lower_bound(std::make_pair(x+1, x+1)));
        auto [l, u] = *it;
        return l <= x && x <= u;
    }

    // 追加した関数
    bool erase(int x) {
        nums[x] = 0;
        auto nit = s.lower_bound(std::make_pair(x+1, x+1));
        auto it = std::prev(nit);
        auto [l, u] = *it;
        if (l == x && x == u) s.erase(it);
        else if(l == x) {
            s.erase(it);
            s.emplace(x+1, u);
        }
        else if(u == x) {
            s.erase(it);
            s.emplace(l, x-1);
        }
        else if(l < x && x < u){
            s.erase(it);
            s.emplace(l, x-1);
            s.emplace(x+1, u);
        }
        else return false;
        return true;
    }

    bool insert(int x) {
        nums[x]++;
        auto nit = s.lower_bound(std::make_pair(x+1, x+1));
        auto it = std::prev(nit);
        auto [l, u] = *it;
        auto [nl, nu] = *nit;
        if (l <= x && x <= u) return false;
        if (u == x-1) {
            if (nl == x+1) {
                s.erase(it);
                s.erase(nit);
                s.emplace(l, nu);
            } else {
                s.erase(it);
                s.emplace(l, x);
            }
        } else {
            if (nl == x+1) {
                s.erase(nit);
                s.emplace(x, nu);
            } else {
                s.emplace(x, x);
            }
        }
        return true;
    }
 
    int mex(int x = 0) const {
        auto [l, u] = *std::prev(s.lower_bound(std::make_pair(x+1, x+1)));
        if (l <= x && x <= u) {
            return u+1;
        } else {
            return x;
        }
    }

    void view() {
        for(auto [u, v] : s) {
            cerr << "[" << u << "," << v << "] ";
        }
        cerr << "\n";
    }

    bool reduce(int x, int y) {
        assert(nums[x] >= y);
        nums[x] -= y;
        if(nums[x] == 0) return erase(x);
        return true;
    }

    bool reduce(int x) {
        return reduce(x, 1);
    }
};
 
int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];

    nekoset s;
    rep(i, 0, m) {
        s.insert(a[i]);
    }

    int ans = s.mex();
    rep(i, 0, n-m){
        s.insert(a[i+m]);
        s.reduce(a[i]);
        ans = min(ans, s.mex());
    }
    // set<pair<int,int>> s の中身を見てdebugしたい
    s.view();
    cout << ans << endl;
    return 0;
}
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define SZ(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> Pii;
typedef pair <ll, int> Pli;
typedef unsigned long long ull;
const double eps = 1e-8;
const int mod = (int)(1e9) + 7;
const int inf = 0x7fffffff;
const int N = (int)(2e6) + 7;

int n, m, a[N], g[N];
ll t[N];
int lowbit(int x) {
    return x & -x;
}
void upd(int x, ll v) {
    while (x <= n) {
        t[x] += v;
        x += lowbit(x);
    }
}
ll sum(int x) {
    if (x <= 0) return 0;
    ll res = 0;
    while (x) {
        res += t[x];
        x -= lowbit(x);
    }
    return res;
}
ll query(int x) {
    return sum(x) - sum(x - 1);
}

int main() {

    // freopen("in.in", "r", stdin);
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int res = n + 1;
    for (int i = 1, j = 1; i <= n; ++i) {
        if (++g[a[i] + 1] == 1) upd(a[i] + 1, 1);
        while (i - j + 1 > m) {
            if (!--g[a[j] + 1]) 
                upd(a[j] + 1, -1);
            ++j;
        }
        // printf("[%d, %d]", i, j);
        if (i - j + 1 != m) continue;
        int L = 0, R = n + 1;
        while (L < R) {
            int mid = (L + R) / 2;
            if (sum(mid + 1) != mid + 1) R = mid;
            else L = mid + 1;
        }
        // printf("(%d, %d:  %d)", j, i, L);
        res = min(res, L);
    }
    printf("%lld", res);

    return 0;
}