<Problem>
http://ntucoder.net/Problem/Details/5539
Uses Crt;
type ToaDo = Record
x,y:longint;
end;
Var n,m,x1,x2,y1,y2:longint;
i,j:longint;
b,k:longint;
Q:array[1..10000000] of ToaDo; sx,sy:longint; soluong:longint;
a:array[-100..1001,-100..1001] of integer;
Procedure LanToa;
begin
b:=1; k:=1;
Q[1].x:=x1;
Q[1].y:=y1;
while b<=k do
begin
sx:=Q[b].x;
sy:=Q[b].y;
soluong:=a[sx,sy];
//--1
if (sx+2<=n) and (sy+1<=m) and (a[sx+2,sy+1]=0) then
begin
a[sx+2,sy+1]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx+2; Q[k].y:=sy+1;
//-----
if (sx+2=x2) and (sy+1=y2) then exit;
end;
//--2
if (sx+1<=n) and (sy+2<=m) and (a[sx+1,sy+2]=0) then
begin
a[sx+1,sy+2]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx+1; Q[k].y:=sy+2;
//-----
if (sx+1=x2) and (sy+2=y2) then exit;
end;
//--3
if (sx-1>=1) and (sy+2<=m) and (a[sx-1,sy+2]=0) then
begin
a[sx-1,sy+2]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx-1; Q[k].y:=sy+2;
//-----
if (sx-1=x2) and (sy+2=y2) then exit;
end;
//--4
if (sx-2>=1) and (sy+1<=m) and (a[sx-2,sy+1]=0) then
begin
a[sx-2,sy+1]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx-2; Q[k].y:=sy+1;
//-----
if (sx-2=x2) and (sy+1=y2) then exit;
end;
//--5
if (sx-2>=1) and (sy-1>=1) and (a[sx-2,sy-1]=0) then
begin
a[sx-2,sy-1]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx-2; Q[k].y:=sy-1;
//-----
if (sx-2=x2) and (sy-1=y2) then exit;
end;
//--6
if (sx-1>=1) and (sy-2>=1) and (a[sx-1,sy-2]=0) then
begin
a[sx-1,sy-2]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx-1; Q[k].y:=sy-2;
//-----
if (sx-1=x2) and (sy-2=y2) then exit;
end;
//--7
if (sx+1<=n) and (sy-2>=1) and (a[sx+1,sy-2]=0) then
begin
a[sx+1,sy-2]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx+1; Q[k].y:=sy-2;
//-----
if (sx+1=x2) and (sy-2=y2) then exit;
end;
//--8
if (sx+2<=n) and (sy-1>=1) and (a[sx+2,sy-1]=0) then
begin
a[sx+2,sy-1]:=soluong+1;
//-----
inc(k);
Q[k].x:=sx+2; Q[k].y:=sy-1;
//-----
if (sx+2=x2) and (sy-1=y2) then exit;
end;
//---Xet Tiep
inc(b);
end;
end;
Begin
readln(n,m);
readln(x1,y1);
readln(x2,y2);
for i:=1 to n do
for j:=1 to m do a[i,j]:=0;
LanToa;
if a[x2,y2]=0 then writeln(-1) else writeln(a[x2,y2]);
readln;
End.