#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
int T[501][501];
bool odw[501][501];
int n,w;
void dfs(int x,int y,int kary){
odw[x][y]=1;
if(0<=x-1&&odw[x-1][y]==0){
if(T[x-1][y]<w) dfs(x-1,y,kary);
else if(kary>0) dfs(x-1,y,kary-1);
}
if(x+1<n&&odw[x+1][y]==0){
if(T[x+1][y]<w) dfs(x+1,y,kary);
else if(kary>0) dfs(x+1,y,kary-1);
}
if(0<=y-1&&odw[x][y-1]==0){
if(T[x][y-1]<w) dfs(x,y-1,kary);
else if(kary>0) dfs(x,y-1,kary-1);
}
if(y+1<n&&odw[x][y+1]==0){
if(T[x][y+1]<w) dfs(x,y+1,kary);
else if(kary>0) dfs(x,y+1,kary-1);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int a,b,c,d,kary;
int p=-1,k=0,sr;
cin>>n>>kary>>a>>b>>c>>d;
a--; b--; c--; d--;
if(abs(a-c)+abs(b-d)<kary){
cout<<"TRIV";
return 0;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>T[i][j];
if(k<T[i][j]) k=T[i][j];
}
}
if(a==c&&b==d){
cout<<T[a][b];
return 0;
}
k++;
while(k-p>1){
w=(p+k)/2;
for(int i=0;i<n;i++) for(int j=0;j<n;j++) odw[i][j]=0;
if(T[a][b]>=w) dfs(a,b,kary-1);
else dfs(a,b,kary);
if(odw[c][d]==1) k=w;
else p=w;
}
cout<<p;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBGIGZpcnN0IAojZGVmaW5lIFMgc2Vjb25kCnVzaW5nIG5hbWVzcGFjZSBzdGQ7IAppbnQgVFs1MDFdWzUwMV07CmJvb2wgb2R3WzUwMV1bNTAxXTsKaW50IG4sdzsKCnZvaWQgZGZzKGludCB4LGludCB5LGludCBrYXJ5KXsKICAgIG9kd1t4XVt5XT0xOwogICAgaWYoMDw9eC0xJiZvZHdbeC0xXVt5XT09MCl7CiAgICAgICAgaWYoVFt4LTFdW3ldPHcpICAgIGRmcyh4LTEseSxrYXJ5KTsKICAgICAgICBlbHNlIGlmKGthcnk+MCkgICAgZGZzKHgtMSx5LGthcnktMSk7CiAgICB9CiAgICBpZih4KzE8biYmb2R3W3grMV1beV09PTApewogICAgICAgIGlmKFRbeCsxXVt5XTx3KSAgIGRmcyh4KzEseSxrYXJ5KTsKICAgICAgICBlbHNlIGlmKGthcnk+MCkgICBkZnMoeCsxLHksa2FyeS0xKTsKICAgIH0KICAgIGlmKDA8PXktMSYmb2R3W3hdW3ktMV09PTApewogICAgICAgIGlmKFRbeF1beS0xXTx3KSAgICBkZnMoeCx5LTEsa2FyeSk7CiAgICAgICAgZWxzZSBpZihrYXJ5PjApICAgIGRmcyh4LHktMSxrYXJ5LTEpOwogICAgfQogICAgaWYoeSsxPG4mJm9kd1t4XVt5KzFdPT0wKXsKICAgICAgICBpZihUW3hdW3krMV08dykgICAgZGZzKHgseSsxLGthcnkpOwogICAgICAgIGVsc2UgaWYoa2FyeT4wKSAgICBkZnMoeCx5KzEsa2FyeS0xKTsKICAgIH0KfQoKaW50IG1haW4oKXsKCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CgljaW4udGllKDApOwogICAgaW50IGEsYixjLGQsa2FyeTsKICAgIGludCBwPS0xLGs9MCxzcjsKICAgIGNpbj4+bj4+a2FyeT4+YT4+Yj4+Yz4+ZDsKICAgIGEtLTsgYi0tOyBjLS07IGQtLTsKICAgIGlmKGFicyhhLWMpK2FicyhiLWQpPGthcnkpewogICAgCWNvdXQ8PCJUUklWIjsKICAgIAlyZXR1cm4gMDsKICAgIH0KICAgIGZvcihpbnQgaT0wO2k8bjtpKyspewogICAgICAgIGZvcihpbnQgaj0wO2o8bjtqKyspewogICAgICAgICAgICBjaW4+PlRbaV1bal07CiAgICAgICAgICAgIGlmKGs8VFtpXVtqXSkgaz1UW2ldW2pdOwogICAgICAgIH0KICAgIH0KICAgIGlmKGE9PWMmJmI9PWQpewogICAgCWNvdXQ8PFRbYV1bYl07CiAgICAJcmV0dXJuIDA7CiAgICB9CiAgICBrKys7CiAgICB3aGlsZShrLXA+MSl7CiAgICAgICAgdz0ocCtrKS8yOwogICAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspICAgIGZvcihpbnQgaj0wO2o8bjtqKyspICAgIG9kd1tpXVtqXT0wOwogICAgICAgIGlmKFRbYV1bYl0+PXcpIGRmcyhhLGIsa2FyeS0xKTsKICAgICAgICBlbHNlIGRmcyhhLGIsa2FyeSk7CiAgICAgICAgaWYob2R3W2NdW2RdPT0xKSAgICAgaz13OwogICAgICAgIGVsc2UgICAgcD13OwogICAgfQogICAgY291dDw8cDsKfQ==