#include <bits/stdc++.h>
using namespace std;
ifstream f("car.in");
ofstream g("car.out");
const int oo = 1<<30;
int n,m,ip,jp,is,js,a[502][502][8];
int depI[8]={-1,-1,0,1,1, 1, 0,-1};
int depJ[8]={ 0, 1,1,1,0,-1,-1,-1};
int rotL[8]={7,0,1,2,3,4,5,6};
int rotR[8]={1,2,3,4,5,6,7,0};
queue<tuple<int,int,int,int>> q,qaux;
int main()
{
f>>n>>m>>ip>>jp>>is>>js;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
int x;
f>>x;
if(x==0)
for(int k=0; k<8; k++)
a[i][j][k]=oo;
}
if(a[ip][jp][0]==0||a[is][js][0]==0)
{
g<<"-1\n";
return 0;
}
if(is==ip&&js==jp)
{
g<<"0\n";
return 0;
}
for(int k=0;k<8;k++)
{
a[ip][jp][k]=0;
q.push(make_tuple(0,k,ip,jp));
}
while(q.size())
{
int cst,dir,i,j;
tie(cst,dir,i,j)=q.front();
q.pop();
/// sunt intr-o anumita pozitie pozitionat pe o anumita directie si am ajuns acolo cu un anumit cost
/// aseasta stare are cel mai mic cost disponibil
/// din aceasta pozitie am 3 optiuni
///-> avansez fara sa fac curba => am acelasi cost
///-> fac 45 grade dreapta => cresc costul cu 1
///-> fac 45 grade stanga => cresc costul cu 1
/// ajung intr-o noua stare pe care o iau in considerare doar daca imi imbunatateste costul
/// VARIANTA 1 -> ma misc
int I=i+depI[dir];
int J=j+depJ[dir];
if(cst<a[I][J][dir])
{
a[I][J][dir]=cst;
q.push(make_tuple(cst,dir,I,J));
}
int DIR=rotL[dir];
if(cst+1<a[i][j][DIR])
{
a[i][j][DIR]=cst+1;
qaux.push(make_tuple(cst+1,DIR,i,j));
}
DIR=rotR[dir];
if(cst+1<a[i][j][DIR])
{
a[i][j][DIR]=cst+1;
qaux.push(make_tuple(cst+1,DIR,i,j));
}
if(q.size()==0)
{
while(qaux.size())
{
q.push(qaux.front());
qaux.pop();
}
}
}
int sol=a[is][js][0];
for(int k=1;k<8;k++)
sol=min(sol,a[is][js][k]);
if(sol==oo)
sol=-1;
g<<sol;
return 0;
}