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 Ruby (2.7.1) (judge verdict: Wrong Answer)

def map(m)
  return
  $stderr.puts "+ #{m}"
  S.each do |s|
    $stderr.puts "| #{s.join}"
  end
  $stderr.puts
end

H, W = $stdin.gets.split.map {|i| i.to_i }
Ch, Cw = $stdin.gets.split.map {|i| i.to_i }
Dh, Dw = $stdin.gets.split.map {|i| i.to_i }
S = []
0.upto(H - 1) do |h|
  S[h] = $stdin.gets.strip.split(//)
end

def set(h, w, v)
  S[h - 1][w - 1] = v
end

def get(h, w)
  if h < 1 || w < 1 || h > H || w > W
    return nil
  end
  return S[h - 1][w - 1]
end

def step(pos_list, clear_list, ch, cw, c)
  v = get(ch, cw)
  if v != '.'
    return v
  end
  set(ch, cw, c)
  pos_list.push([ch, cw])
  clear_list.push([ch, cw]) if clear_list
  return c
end

def walk(walk_list, ch0, cw0)
  c = get(ch0, cw0)
  pos_list = [[ch0, cw0]]
  while !pos_list.empty?
    ch, cw = pos_list.pop
    offset = walk_list.length - 1
    c1 = step(pos_list, walk_list, ch + 1, cw, c)
    c2 = step(pos_list, walk_list, ch - 1, cw, c)
    c3 = step(pos_list, walk_list, ch, cw + 1, c)
    c4 = step(pos_list, walk_list, ch, cw - 1, c)
    if c == c1 && c == c2 && c == c3 && c == c4
      walk_list.delete_at(offset)
    end
  end
end

def warp(warp_list, ch0, cw0)
  c = get(ch0, cw0) + 1
  -2.upto(2) do |oh|
    -2.upto(2) do |ow|
      next if (oh == 0 && ow.abs <= 1) || (ow == 0 && oh.abs <= 1)
      step(warp_list, nil, ch0 + oh, cw0 + ow, c)
    end
  end
end

if get(Ch, Cw) != '.' || get(Dh, Dw) != '.'
  puts "-1"
  exit
end

c = 0
map "-1"
set(Ch, Cw, c)
walk_list = [[Ch, Cw]]
walk(walk_list, Ch, Cw)
map c
while (dc = get(Dh, Dw)) == '.'
  c += 1
  warp_list = []
  walk_list.each do |pos|
    warp(warp_list, *pos)
  end
  if warp_list.empty?
    dc = -1
    break
  end
  walk_list = []
  warp_list.each do |pos|
    walk(walk_list, *pos)
  end
  if walk_list.empty?
    walk_list = warp_list
  end
end
puts dc