Problem:
Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>

int zahyo(int y, int x, int w){
    return y * w + x;
}



int check_group(int y, int x, int group, int h, int w, int *s, int dh, int dw){
    *(s + (zahyo(y, x, w))) = group;

    if(x + 1 < w){
        if(*(s + (zahyo(y, x + 1, w))) == 0){
            if(check_group(y, x + 1, group, h, w, s, dh, dw) > 0)
                return group;
        }
    }
    if(y + 1 < h){
        if(*(s + (zahyo(y + 1, x, w))) == 0){
            if(check_group(y + 1, x, group, h, w, s, dh, dw) > 0)
                return group;
        }
    }
    if(x - 1 >= 0){
        if(*(s + (zahyo(y, x - 1, w))) == 0){
            if(check_group(y, x - 1, group, h, w, s, dh, dw) > 0)
                return group;
        }
    }
    if(y - 1 >= h){
        if(*(s + (zahyo(y - 1, x, w))) == 0){
            if(check_group(y - 1, x, group, h, w, s, dh, dw) > 0)
                return group;
        }
    }


    if(y == dh && x == dw)
        return group;
    else
        return -1;

}



int main(void){
    int h, w, ch, cw, dh, dw;

    int *s;
    int *kaisu;

    int i, j, k, l;

    int group;
    int oya = 1;
    int owari = 1;


    scanf("%d", &h);
    scanf("%d", &w);
    scanf("%d", &ch);
    scanf("%d", &cw);
    scanf("%d", &dh);
    scanf("%d", &dw);

    ch -= 1;
    cw -= 1;
    dh -= 1;
    dw -= 1;

    s = (int*)malloc(sizeof(int) * h * w);
    kaisu = (int*)malloc(sizeof(int) * h * w);

    for(i = 0; i < h; i++){
        char c[1001];
        scanf("%s", c);
        j = 0;
        while(c[j] != '\0'){
            if(c[j] == '#')
                s[i * w + j] = -1;
            else
                s[i * w + j] = 0;
            j++;
        }
    }


    if(check_group(ch, cw, 1, h, w, s, dh, dw) > 0){
        printf("%d\n", 0);
        return 0;
    }


    group = 1;
//    oya = 1;
    kaisu[1] = 0;
    owari = 1;


    while(owari){
        owari  = 0;
        for(i = 0; i < h; i++){
            for(j = 0; j < w; j++){
                if(s[zahyo(i, j, w)] == group){
                    for(k = -2; k <= 2; k++){
                        for(l = -2; l <= 2; l++){
                            if(i + k >= 0 && i + k < h && j + l >= 0 && j + l < w){
                                if(s[zahyo(i + k, j + l, w)] == 0){
                                    owari = 1;
                                }
                                if(!(k == 0 && l == 0) && s[zahyo(i + k, j + l, w)] == 0){
                                    int result;
                                    result = check_group(i + k, j + l, group + 1, h, w, s, dh, dw);
//                                    kaisu[group] = kaisu[oya] + 1;
                                    if(result > 0){
                                        printf("%d\n", group);
                                        return 0;
                                    }
                                }
                            }
                        }
                    }
                }
//                printf("%d ", s[zahyo(i, j, w)]);



            }

//            printf("\n");
        }
//       printf("\n");
//            xxx += 1;

             group += 1;

        oya += 1;
    }

//    for(i = 0; i < h; i++){
//        for(j = 0; j < w; j++)
//            printf("%d ", s[zahyo(i, j, w)]);
//        printf("\n");
//    }

        printf("%d\n", -1);
        return 0;





    free(s);
    return 0;
}