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

#define rep(i, n) for (int i=0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;

int main(){
    int n, m; cin >> n >> m;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    vector<vector<int>> pos(n, vector<int> {-1});
    rep(i, n) pos[a[i]].push_back(i);
    rep(i, n) pos[i].push_back(n);
    int ans;
    rep(i, n) {
        bool flag = false;
        rep(j, pos[i].size()-1) {
            //cout << pos[i][j] << " ";
            if (pos[i][j+1] - pos[i][j] > m) flag = true;
        }
        //cout << pos[i].back() << endl;
        if (flag) {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}


#include<stdio.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static int IN(void)
{
  int x=0,f=1,c;while(c=getchar(),c<48||c>57){if(c==45)f=-f;}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f*x;
}
static void OUT(int x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x-x/10*10+48);}
int main(void)
{
  int N=IN(),K=IN(),ext=0,lim=(K*K)/2+1,ng=-1,ok=1.4e9,mid=0,A[800][800],sum[801][801];
  rep(i,N)rep(j,N){A[i][j]=IN();sum[i][j]=0;}
  while(ng+1<ok)
  {
    mid=(ng+ok)/2;
    rep(i,N)rep(j,N)
    {
      sum[i+1][j+1]=sum[i+1][j]+sum[i][j+1]-sum[i][j];
      if(A[i][j]>mid){sum[i+1][j+1]++;}
    }
    ext=0;
    rep(i,N-K+1)rep(j,N-K+1)if((sum[i+K][j+K]+sum[i][j]-sum[i][j+K]-sum[i+K][j])<lim){ext=1;}
    if(ext)ok=mid;else ng=mid;
  }
  OUT(ok);
}