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>

#define Q_LENGTH 2000

void show_maze(int S[][1000], int height, int width, FILE *fp) {
  int h, w;

  for(h = 0; h < height; h++) {
    for(w = 0; w < width; w++) {
      if(S[h][w] == 0) {
        fputc(' ', fp);
        fputc(' ', fp);
      }else
        fprintf(fp, "%2d", S[h][w]);
    }
    fputc('\n', fp);
  }
}

int main(void){
  typedef struct{
	int h;
	int w;
  } point;
  
  int height, width;
  point start;
  point goal;
  int S[1000][1000];
  point queue[Q_LENGTH];
  int qhead = 0, qtale = 0;
  int h, w;
  point current;
  int warp = 0;
fprintf(stderr,"hello\n");
 
  scanf("%d%d", &height, &width);
  scanf("%d%d", &start.h, &start.w);
  scanf("%d%d", &goal.h, &goal.w);
  start.h--;
  start.w--;
  goal.h--;
  goal.w--;
  fprintf(stderr,"hello\n");
  getchar();
  for(h = 0; h < height; h++){
    for(w = 0; w < width; w++){
      S[h][w] = (getchar() == '#');
    }
    getchar();
  }
  
  queue[0] = start;
  qtale = 1;
  S[start.h][start.w] = 1;
  	
  while(current.h != goal.h || current.w != goal.w){
    point tmp[24] = {{0,1}, {1,0}, {0,-1}, {-1, 0}, {2, 2}, {2, 1}, {2, 0}, {2, -1}, {2, -2},{1, 2}, {1, 1}, {1, -1}, {1, -2}, {0, 2}, {0, -2}, {-1, 2}, {-1, 1}, {-1, -1}, {-1, -2}, {-2, 2}, {-2, 1}, {-2, 0}, {-2, -1}, {-2, -2}};
	int i = 0;
    current = queue[qhead];
	qhead = (qhead + 1) %Q_LENGTH;

    fprintf(stderr, "%d, %d\n", current.h, current.w);

    show_maze(S, height, width, stderr);

	for(; i < 4; i++){
      point pt = {current.h + tmp[i].h, current.w + tmp[i].w};

      if(pt.h < 0 || pt.h >= height || pt.w < 0 || pt.w >= width)
        continue;

	  if((!S[pt.h][pt.w]) || S[pt.h][pt.w] > S[current.h][current.w]){
		S[pt.h][pt.w] = S[current.h][current.w];
		queue[qtale] = pt;
		qtale = (qtale + 1)%Q_LENGTH;
	  }
	}

	for(; i < 24; i++){
	  point pt = {current.h + tmp[i].h, current.w + tmp[i].w};
    
      if(pt.h < 0 || pt.h >= height || pt.w < 0 || pt.w >= width)
        continue;

	  if(!S[pt.h][pt.w]){
		S[pt.h][pt.w] = S[current.h][current.w] + 1;
		queue[qtale] = pt;
		qtale = (qtale + 1)%Q_LENGTH;
	  }
	}
    if(qhead == qtale){
      if(current.h == goal.h && current.w == goal.w) {
        fprintf(stderr,"goal\n");
        break;
      } else {
        printf("-1\n");
        return 0;
      }
    }
  }

  printf("%d\n", S[goal.h][goal.w]-1);
  
  return 0;
}