#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<cstdlib>
#define INF 1<<30
#define MAXN 1005
using namespace std;
struct node
{
int x,y,val,dep,dist;
node () { }
node (int a, int b) { x=a; y=b; }
};
node path[MAXN];
struct cmp // 比较方式
{
bool operator () (node a,node b)
{
return (a.val>b.val);
}
};
int sx,sy,tx,ty,nx,ny,map[MAXN][MAXN];
priority_queue<node,vector<node>,cmp> q;
int n,s,t,x[MAXN],y[MAXN],now;
int abs(int a) { return (a<0)?-a:a; }
int getVal(int x,int y,int dep) { return (abs(x-tx)+abs(y-ty)+dep); }
int getMan(int x,int y) { return (abs(x-sx)+abs(y-sy)); }
int check(int x,int y)
{
return ( x>0 && y>0 && x<=map[nx][ny]);
}
void BFS()
{
node s=node(sx,sy); s.dep=0; s.dist=0;
q.push(s);
while (q.empty()!=1)
{
node minNode=q.top();
nx=minNode.x; ny=minNode.y;
if (minNode.x==tx && minNode.y==ty)
{
printf("%d",minNode.dist);
exit(0);
}
q.pop();
if (check(nx,ny+1)==1)
{
node temp=node(nx,ny+1);
temp.dep=getMan(nx,ny+1);
temp.val=getVal(nx,ny+1,temp.dep);
temp.dist=minNode.dist+1;
map[nx][ny+1]=temp.dep;
q.push(temp);
}
if (check(nx,ny-1)==1)
{
node temp=node(nx,ny-1);
temp.dep=getMan(nx,ny-1);
temp.val=getVal(nx,ny-1,temp.dep);
temp.dist=minNode.dist+1;
map[nx][ny-1]=temp.dep;
q.push(temp);
}
if (check(nx+1,ny)==1)
{
node temp=node(nx+1,ny);
temp.dep=getMan(nx+1,ny);
temp.val=getVal(nx+1,ny,temp.dep);
temp.dist=minNode.dist+1;
map[nx+1][ny]=temp.dep;
q.push(temp);
}
if (check(nx-1,ny)==1)
{
node temp=node(nx-1,ny);
temp.dep=getMan(nx-1,ny);
temp.val=getVal(nx-1,ny,temp.dep);
temp.dist=minNode.dist+1;
map[nx-1][ny]=temp.dep;
q.push(temp);
}
}
}
void init()
{
freopen("AStar.in","r",stdin);
freopen("AStar.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) { scanf("%d",&map[i][j]); if (map[i][j]==1) map[i][j]=INF; }
scanf("%d %d %d %d",&sx,&sy,&tx,&ty);
}
int main()
{
init();
BFS();
return 0;
}