import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import javax.sql.PooledConnection;
import javax.swing.JEditorPane;
public class Grasshop {
static void vypis(int[][] pole) {
for (int i = 0; i < pole.length; i++) {
for (int j = 0; j < pole.length; j++) {
System.
out.
print(pole
[i
][j
] + " "); }
}
}
static void skakaj(Queue<Integer> rad, int aktualnyx, int aktualnyy,
int aktualnyh, int[][] poleVysledkov) {
if (poleVysledkov[aktualnyx + 2][aktualnyy + 1] > aktualnyh + 1
|| poleVysledkov[aktualnyx + 2][aktualnyy + 1] == -1) {
rad.add(aktualnyx + 2);
rad.add(aktualnyy + 1);
rad.add(aktualnyh + 1);
}
if (poleVysledkov[aktualnyx + 1][aktualnyy + 2] > aktualnyh + 1
|| poleVysledkov[aktualnyx + 1][aktualnyy + 2] == -1) {
rad.add(aktualnyx + 1);
rad.add(aktualnyy + 2);
rad.add(aktualnyh + 1);
}
if (poleVysledkov[aktualnyx - 2][aktualnyy - 1] > aktualnyh + 1
|| poleVysledkov[aktualnyx - 2][aktualnyy - 1] == -1) {
rad.add(aktualnyx - 2);
rad.add(aktualnyy - 1);
rad.add(aktualnyh + 1);
}
if (poleVysledkov[aktualnyx - 1][aktualnyy - 2] > aktualnyh + 1
|| poleVysledkov[aktualnyx - 1][aktualnyy - 2] == -1) {
rad.add(aktualnyx - 1);
rad.add(aktualnyy - 2);
rad.add(aktualnyh + 1);
}
// -----------------------
if (poleVysledkov[aktualnyx - 2][aktualnyy + 1] > aktualnyh + 1
|| poleVysledkov[aktualnyx - 2][aktualnyy + 1] == -1) {
rad.add(aktualnyx - 2);
rad.add(aktualnyy + 1);
rad.add(aktualnyh + 1);
}
if (poleVysledkov[aktualnyx - 1][aktualnyy + 2] > aktualnyh + 1
|| poleVysledkov[aktualnyx - 1][aktualnyy + 2] == -1) {
rad.add(aktualnyx - 1);
rad.add(aktualnyy + 2);
rad.add(aktualnyh + 1);
}
if (poleVysledkov[aktualnyx + 2][aktualnyy - 1] > aktualnyh + 1
|| poleVysledkov[aktualnyx + 2][aktualnyy - 1] == -1) {
rad.add(aktualnyx + 2);
rad.add(aktualnyy - 1);
rad.add(aktualnyh + 1);
}
if (poleVysledkov[aktualnyx + 1][aktualnyy - 2] > aktualnyh + 1
|| poleVysledkov[aktualnyx + 1][aktualnyy - 2] == -1) {
rad.add(aktualnyx + 1);
rad.add(aktualnyy - 2);
rad.add(aktualnyh + 1);
}
}
/**
* @param args
*/
public static void main
(String[] args
) { final int posun = 2;
Scanner s
= new Scanner
(System.
in); int riadkov, stlpcov, sx, sy, kx, ky;
int[][] poleVysledkov;
boolean naslaSaCesta = false;
int aktualnyx, aktualnyy, aktualnyh;
while (s.hasNextLine()) {
riadkov = s.nextInt();
stlpcov = s.nextInt();
sx = s.nextInt() + posun - 1;
sy = s.nextInt() + posun - 1;
kx = s.nextInt() + posun - 1;
ky = s.nextInt() + posun - 1;
poleVysledkov = new int[riadkov + 2 * posun][stlpcov + 2 * posun];
// daj minus jednotky
for (int i = 0; i < poleVysledkov.length; i++) {
for (int j = 0; j < poleVysledkov.length; j++) {
poleVysledkov[i][j] = -2;
}
}
for (int i = posun; i < poleVysledkov.length - posun; i++) {
for (int j = posun; j < poleVysledkov.length - posun; j++) {
poleVysledkov[i][j] = -1;
}
}
if(poleVysledkov.length<9)
{
//prehladaj do sirky
poleVysledkov[sx][sy] = 0;
Queue<Integer> rad=new LinkedList<Integer>();
rad.add(sx);
rad.add(sy);
rad.add(0);
naslaSaCesta=false;
while(!rad.isEmpty())
{
aktualnyx=rad.poll();
aktualnyy=rad.poll();
aktualnyh=rad.poll();
if (aktualnyx==kx&&aktualnyy==ky)
{
naslaSaCesta=true;
System.
out.
println(aktualnyh
); break;
}
if (poleVysledkov[aktualnyx][aktualnyy]>aktualnyh||poleVysledkov[aktualnyx][aktualnyy]==-1)
{
poleVysledkov[aktualnyx][aktualnyy]=aktualnyh;
}
skakaj(rad, aktualnyx, aktualnyy, aktualnyh, poleVysledkov);
}
if (!naslaSaCesta
) System.
out.
println("impossible");
}else {
// este vymenit + zaciatocny nula
int pom;
if (sx > kx && sy > ky) {
// vymeni
pom = sx;
sx = kx;
kx = pom;
pom = sy;
sy = ky;
ky = pom;
}
poleVysledkov[sx][sy] = 0;
int[][] posuvnepole = new int[5][5];
int[] pole = { 4, 1, 2, 1, 4 };
posuvnepole[0] = pole;
int[] pole1 = { 1, 2, 3, 2, 1 };
posuvnepole[1] = pole1;
int[] pole2 = { 2, 3, 0, 3, 2 };
posuvnepole[2] = pole2;
posuvnepole[3] = pole1;
posuvnepole[4] = pole;
for (int i = posun; i < poleVysledkov.length - posun; i++) {
for (int j = posun; j < poleVysledkov.length - posun; j++) {
if (poleVysledkov[i][j] != -2)
// pre policko pricitaj mriezku
for (int j2 = -2; j2 <= 2; j2++) {
for (int k = -2; k <= 2; k++) {
if (poleVysledkov[i + j2][j + k] > poleVysledkov[i][j]
+ posuvnepole[j2+2][k+2]
|| poleVysledkov[i + j2][j + k] == -1) {
poleVysledkov[i + j2][j + k] = poleVysledkov[i][j]
+ posuvnepole[j2+2][k+2];
}
}
}
}
}
//vypis(poleVysledkov);
System.
out.
println(poleVysledkov
[kx
][ky
]);} }
}
}