#include <queue>
#include <iostream>
using namespace std;
struct stu{int x, y, step;};
int n, m, a[105][105], sx, sy, ex, ey;
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};
void print(int a[][105], int n, int m)
{
cout << "\n";
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << a[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
bool check(int x, int y)
{
if (x < 1 || x > n)return 0;
if (y < 1 || y > m)return 0;
if (a[x][y] == 1)return 0;
return 1;
}
int bfs()
{
queue<stu> q;
q.push(stu{sx, sy, 0});
while (!q.empty())
{
stu f = q.front();
q.pop();
if (f.x == ex && f.y == ey)
{
return f.step;
}
for (int i = 1; i <= 4; i++)
{
int k = f.step + 1;
int nx = f.x;
int ny = f.y;
do
{
nx += dx[i];
ny += dy[i];
if (!check(nx, ny))
{
break;
}
a[nx][ny] = 1;
q.push(stu{nx, ny, k});
}while (1);
}
}
return 0;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
}
}
cin >> sx >> sy >> ex >> ey;
a[sx][sy] = 1;
cout << bfs() - 1;
return 0;
}