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 Lua (LuaJIT 2.1.0) (judge verdict: Time Limit Exceeded)

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local pow2 = {1}
for i = 2, 28 do pow2[i] = pow2[i - 1] * 2 end
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = pow2[i]
    for j = 0, cnt - 1 do
      self.stage[cnt + j] = self.func(self.stage[(cnt + j) * 2], self.stage[(cnt + j) * 2 + 1])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
  end
  self.stagenum = stagenum
  for i = 1, mul * 2 - 1 do self.stage[i] = emptyvalue end
  for i = 1, n do self.stage[mul + i - 1] = i end
  self:updateAll()
end
SegTree.update = function(self, idx)
  idx = idx + pow2[self.stagenum] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mfl(idx / 2)
    local rem = dst * 4 + 1 - idx
    self.stage[dst] = self.func(self.stage[idx], self.stage[rem])
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local h, w = io.read("*n", "*n", "*l")
local sh, sw = io.read("*n", "*n", "*l")
local th, tw = io.read("*n", "*n", "*l")
local dot = string.byte(".")
local map = {}
local len = {}
local asked = {}
local inf = 1000000007
for i = 1, h do
  local s = io.read()
  for j = 1, w do
    local idx = (i - 1) * w + j
    map[idx] = s:byte(j) == dot
    len[idx] = inf
    asked[idx] = false
  end
end
local n = h * w
asked[n + 1] = true
len[(sh - 1) * w + sw] = 0

local function mergefunc(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  else
    return len[x] < len[y] and x or y
  end
end

local st = SegTree.new(n, mergefunc, n + 1)

local function walk(src, dst, cost)
  if map[dst] then
    if len[src] + cost < len[dst] then
      len[dst] = len[src] + cost
      st:update(dst)
    end
  end
end

for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  asked[src] = true
  st:update(src)
  local src_h = mce(src / w)
  local src_w = src - (src_h - 1) * w
  do
    if 2 < src_h then walk(src, src - 2 * w, 1) end
    if 1 < src_h then walk(src, src - w, 0) end
    if src_h < h then walk(src, src + w, 0) end
    if src_h < h - 1 then walk(src, src + 2 * w, 1) end
  end
  if 1 < src_w then
    walk(src, src - 1, 0)
    if 2 < src_h then walk(src, src - 1 - 2 * w, 1) end
    if 1 < src_h then walk(src, src - 1 - w, 1) end
    if src_h < h then walk(src, src - 1 + w, 1) end
    if src_h < h - 1 then walk(src, src - 1 + 2 * w, 1) end
  end
  if src_w < w then
    walk(src, src + 1, 0)
    if 2 < src_h then walk(src, src + 1 - 2 * w, 1) end
    if 1 < src_h then walk(src, src + 1 - w, 1) end
    if src_h < h then walk(src, src + 1 + w, 1) end
    if src_h < h - 1 then walk(src, src + 1 + 2 * w, 1) end
  end
  if 2 < src_w then
    walk(src, src - 2, 1)
    if 2 < src_h then walk(src, src - 2 - 2 * w, 1) end
    if 1 < src_h then walk(src, src - 2 - w, 1) end
    if src_h < h then walk(src, src - 2 + w, 1) end
    if src_h < h - 1 then walk(src, src - 2 + 2 * w, 1) end
  end
  if src_w < w - 1 then
    walk(src, src + 2, 1)
    if 2 < src_h then walk(src, src + 2 - 2 * w, 1) end
    if 1 < src_h then walk(src, src + 2 - w, 1) end
    if src_h < h then walk(src, src + 2 + w, 1) end
    if src_h < h - 1 then walk(src, src + 2 + 2 * w, 1) end
  end
end

local ret = len[(th - 1) * w + tw]
print(ret < inf and ret or -1)