//
// main.cpp
// car
//
// Created by Andu Andu on 11/09/2019.
// Copyright © 2019 Andu Andu. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
using namespace std;
ifstream f ("car.in");
ofstream g ("car.out");
int di[8] = {0,0,1,-1,1,-1,-1,1};
int dj[8] = {1,-1,0,0,1,-1,1,-1};
int ng[8] = {90,270,0,180,45,225,135,315};
int nk[8] = {9,9,0,0,4,4,3,3};
int directii[100000];
int ing;
int m, n;
int mak[100][100];
int xs, ys, xf, yf;
int i, j;
queue<pair<int, int>> que;
bool ok(int i, int j) {
if(i<1 or j<1 or i>n or j>n) {
return false;
}
if(mak[i][j] == -1) {
return false;
}
return true;
}
int func(int di, int dj) {
return 0;
}
void lee() {
int i,j,iu,ju;
mak[xs][ys] = 1 ;
que.push(make_pair(xs, ys));
while (!que.empty()) {
i = que.front().first;
j = que.front().second;
que.pop();
for(int dir=0;dir<8;dir++){
iu = i + di[dir];
ju = j + dj[dir];
if(ok(iu, ju) and mak[iu][ju] == 0) {
//int gg = ng[directii[max(ing - 1, 0)]];
int gg = ng[dir] - ng[directii[max(ing - 1, 0)]];
if(gg < 0 && gg >= -180) {
gg += 180;
}else if(gg > 180) {
gg -= 180;
}else if(gg < -180) {
gg += 360;
}
if (!(i == xs && j == ys) ) {
if (gg == 0) {
mak[iu][ju] = mak[i][j] + 0 ;
} else if (gg==90) {
mak[iu][ju] = mak[i][j] + 2 ;
} else if (gg==45) {
mak[iu][ju] = mak[i][j] + 3 ;
} else if (gg==135) {
mak[iu][ju] = mak[i][j] + 1 ;
} else if (gg==180) {
mak[iu][ju] = mak[i][j] + 4 ;
}
} else {
gg=0;
mak[iu][ju] = mak[i][j] ;
}
directii[ing] = dir;
//cout<<ng[directii[max(ing - 1, 0)]]<<" ";
que.push(make_pair(iu, ju));
ing++;
}
}
}
}
int main() {
f>>n>>m;
f>>xs>>ys>>xf>>yf;
for (i=1; i<=n; i++) {
for(j=1; j<=m; j++) {
f>>mak[i][j];
if (mak[i][j] == 1) {
mak[i][j] = -3;
}
}
}
lee();
/*for (i=1; i<=n; i++) {
for(j=1; j<=m; j++) {
if(!(i==xs & j==ys) and mak[i][j] >= 0)
g<<mak[i][j]-1<<" ";
else if(i==xs & j==ys)
g<<0<<" ";
else if(mak[i][j] < 0) {
g<<"_ ";
}
}
g<<"\n";
}*/
g<<mak[xf][yf] - 1;
return 0;
}