#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std; 

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef vector<bool> VB;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) LL((a).size())

#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ROF(i,a,b) for(LL i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL LARGE_INT = 1e9+100;
const LL INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const LL MOD = 1e9+7;

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

LL modpow(LL a, LL n) {
    LL res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

void Main()
{
    LL n;cin>>n;

    LL result = INF_LL;
    VL a(n),b(n);
    REP(i,n){        
        cin>>a[i]>>b[i];
        result = min(result,a[i]+b[i]);
    }

    REP(i,n){
        REP(j,n){
            if(i==j){
                continue;
            }
            result = min(result,max(a[i],b[j]));
        }
    }

    cout<<result<<endl;

    
    
	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
    return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cstdio>
#include <cmath>
#include <bitset>


#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

const int INF = 100100100;

/*
ll gcd(ll a, ll b){
	//log max(a,b)
	if (a % b == 0){
		return(b);
	}
	else{
		return(gcd(b, a % b));
	}
}

ll lcm(ll a, ll b){
	return a * b / gcd(a, b);
}
*/ 
//gcd lcm



int main() {//////////いもす
	int n, k;
	cin >> n >> k;
	vector<vector<int>>a(n, vector<int>(n));
	rep(i, n)rep(j, n)cin >> a[i][j];
	int L = k * k / 2 + 1;
	int wa = -1, ac = 1001001001;
	while (wa + 1 < ac) {
		int wj = (wa + ac) / 2;
		bool ok = false;
		{
			vector<vector<int>> s(n + 1, vector<int>(n + 1));
			rep(i, n)rep(j, n) s[i + 1][j + 1] = a[i][j]>wj?1:0;
			rep(i, n+1)rep(j, n)s[i][j+1] += s[i][j];
			rep(i, n)rep(j, n+1)s[i + 1][j] += s[i][j];
			rep(i, n - k + 1)rep(j, n - k + 1) {
				int now = s[i+k][j+k];
				now -= s[i][j + k];
				now -= s[i + k][j];
				now += s[i][j];
				if (now < L)ok = true;
			}
		}
		if (ok)ac = wj; else wa = wj;
	}
	cout << ac << endl;
	return 0;
}