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

#define fastio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output            freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 

template <typename Arg1>
void prn(Arg1&& arg1) { cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prn(args...); }

template <typename Arg1>
void prs(Arg1&& arg1) { cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); }

template <typename Arg1>
void read(Arg1&& arg1) { cin>>arg1; }
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); }

#define int                     long long
#define all(ds)                 ds.begin(), ds.end()
#define uniq(v)                 (v).erase(unique(all(v)),(v).end())
#define gcd(x,y)                __gcd(x,y)
#define rep(i,a,b)              for(int i=a;i<b;i++)
#define precise(x)              cout<<fixed<<setprecision(x)
#define endl                    "\n"

const long long INF = 1e18;
const int32_t MOD = 1e9+7;
const int N = 2e5+5;

int n, m, k, q, r, l, x, y, z;
int a[N], b[N], c[N];
string s, t;
int ans ;

unordered_map<int, int>dp;

int recur(int y){
    

    if(dp.find(y) != dp.end())return dp[y];

    dp[y] = abs(y-x);
    if(y == 1){
        return dp[y];
    }
    if(y%2 == 0){
        dp[y] = min(dp[y], 1 + recur(y/2));
    }
    else{
        dp[y] = min(dp[y], 1 + min(recur(y-1), recur(y+1)));
    }
    return dp[y];

}
void solve(){
    read(x,y);
    // ans = abs(x-y);
    prn(recur(y));
}

signed main()
{
#ifndef ONLINE_JUDGE  
    input_output  
#else   
    fastio
#endif

#ifdef SIEVE
    sieve();
#endif

#ifdef NCR
    init();
#endif

    int t = 1;
    // cin>>t;
    while(t--){
        solve();
    }

    return 0;
}
#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 810;

int a[N][N], sum[N][N];
int n, k, ju;

bool check(int mid) {
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= n; j ++) {
            int t = (a[i][j] > mid);
            sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + t;
        }
    }
    for (int i = k; i <= n; i ++)
    for (int j = k; j <= n; j ++) {
        int t = sum[i][j] - sum[i][j - k] - sum[i - k][j] + sum[i - k][j - k];
        if (t < ju)   return 1;
    }
    return 0;
}

int main() {
    cin >> n >> k;
    ju = k * k / 2 + 1;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++)   cin >> a[i][j];

    int l = 0, r = 1e9;
    while (l < r) {
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l;
    return 0;
}
