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

#define ll long long
#define mtv(kit) cout<<#kit<<" - "<<kit<<"\n";
#define ff first
#define ss second
#define pb push_back
#define rep(i,a,b) for(i=a;i<b;i++)
#define pii pair<ll , ll>
#define all(x) x.begin(),x.end()
#define nl "\n"
#define ump unordered_map

void doit(){
	ll n, m; cin >> n >> m;
	vector<ll>v(n);
	for(ll i = 0; i < n; i++)cin >> v[i];
	set<ll>s;
	ump<ll,ll>mp;
	for(ll i = 0; i <= n; i++){
		s.insert(i);
	}
	for(ll i = 0; i < m; i++){
		mp[v[i]]++;
		s.erase(v[i]);
	}
	ll mex = *s.begin();
	for(ll i = m; i < n; i++){
		mp[v[i - m]]--;
		if(mp[v[i - m]] == 0)
			s.insert(v[i -m]);
		mp[v[i]]++;
		s.erase(v[i]);
		mex = min(mex,*s.begin());
	}
	cout << mex << nl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin >> t;
    for(ll i = 1; i <= t; i++){
        doit();
    }
}

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

#define pb push_back
#define mp make_pair
#define ll long long
#define int ll
#define ld long double
#define reps(i,s,n) for(int i=(s);i<(int)(n);i++) 
#define rep(i,n) reps(i,0,n)
#define rreps(i,s,n) for(int i=(int)(s-1);i>=n;i--) 
#define rrep(i,n) rreps(i,n,0)
#define all(v) (v).begin(),(v).end()
#define mset(a,n) memset(a,n,sizeof(a))
#define eras(v,n) (v).erase(remove(all(v),n),(v).end())
#define uni(v) sort(all(v));(v).erase(unique(all(v)),(v).end())
#define bcnt(i)  __builtin_popcount(i)
#define fi first
#define se second
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>T min(vector<T> &v) { return *min_element(v.begin(),v.end()); }
template<class T>T max(vector<T> &v) { return *max_element(v.begin(),v.end()); }
template<class T>T modpow(T a, T b, T m) { a %= m; T r = 1; while (b > 0) { if (b & 1) r = (r * a) % m; a = (a * a) % m; b >>= 1; } return r; }
ll modinv(ll a,ll m=MOD) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
template<class T>string radixconv(T n, T k){ string s; while(n/k){ s=to_string(n%k)+s; n/=k; } s=to_string(n%k)+s; return s; }

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(11);
    cerr << fixed << setprecision(6);
    int n,k;
    cin >> n >> k;
    vector<vector<int>> a(n,vector<int>(n,0));
    rep(i,n)rep(j,n)cin>>a[i][j];
    int m=(k*k)/2+1;
    int l=-1,r=INF;
    auto f=[&](int mid){
        vector<vector<int>> csum(n+1,vector<int>(n+1,0));
        //rep(i,n)rep(j,n)if(a[i][j]>mid)csum[i+1][j+1];
        //rep(i,n)rep(j,n)csum[i][j+1]+=csum[i][j];
        //rep(i,n)rep(j,n)csum[i+1][j]+=csum[i][j];
        rep(i,n){
			rep(j,n){
				csum[i+1][j+1]=csum[i+1][j]+csum[i][j+1]-csum[i][j];
				if(a[i][j]>mid)csum[i+1][j+1]++;
			}
		}
        bool res=0;
        rep(i,n-k+1)rep(j,n-k+1)if(csum[i+k][j+k]+csum[i][j]-csum[i][j+k]-csum[i+k][j]<m)res=1;
        return res;
    };
    while(r-l>1){
        int mid=(r+l)/2;
        if(f(mid)) r=mid;
        else l=mid;
    }
    cout << r << endl;
    return 0;
}

