#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct Node {
int x, y;
};
int n;
#define N 1000+10
int g[N][N];
int d[N][N];
bool vis[N][N];
const int dx[4] = { 0,0,-1,1 };
const int dy[4] = { -1,1,0,0 };
queue<Node>q;
int bfs(int ix, int iy,int ox,int oy) {
q.push({ix,iy});
vis[ix][iy] = true;
while (!q.empty()) {
auto t = q.front();
q.pop();
if (t.x == ox && t.y == oy) {
return d[t.x][t.y];
}
for (int i = 0; i < 4; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x >= 0 && x < n && y >= 0 && y < n && g[x][y] == 0 && vis[x][y] == false) {
d[x][y] = d[t.x][t.y] + 1;
vis[x][y] = true;
q.push({ x, y });
}
}
}
return -1;
}
int main() {
int n, x1, x2, y1, y2;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> g[i][j];
}
}
cin >> x1 >> y1 >> x2 >> y2;
cout << bfs(x1, y1, x2, y2);
return 0;
}