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: Time Limit Exceeded)

### SNIPPET
  # n = gets.split.map(&:to_i)
  # array = n.times.map { gets.split.map(&:to_i) }
  # [].all?(&:even?)
  # a = [*1..m].repeated_combination(n).to_a
  # [1,2,3,4,5].select { |num| num.even? }  # => [2, 4]
  # ["a","a","b"].group_by(&:itself).map{|k,v| [k, v.count]}.to_h #=> {"a"=>2, "b"=>1}
  # 切り捨て: .floor(2).to_f ,切り上げ： .ceil(2).to_f,四捨五入: round(2)
  # 3.upto(6) do |i|, 6.downto(3) do |i|
  # 公約数125.gcd(100)、公倍数125.lcm(100)
  # PI = Math::PI
  # 高さ = a * Math.sin(w / 180.0 * Math::PI), 底辺 = a * Math.cos(w / 180.0 * Math::PI)
  # def chmax(a, b) a > b ? a : b end
  # INF = Float::INFINITY
  def chmin(a, b) a < b ? a : b end

# require 'pry'

h,w = gets.split.map(&:to_i)
ci,cj = gets.split.map(&:to_i)
di,dj = gets.split.map(&:to_i)

ci -= 1
cj -= 1
di -= 1
dj -= 1

s = []
h.times do |_|
  s << gets.chomp.split("")
end

# @count = 0

# def counter
#   @count += 1
# end

# def print_table(s)
#   s.each do |ele|
#     ele.each do |d|
#       print d
#     end
#     puts ""
#   end
#   puts ""
# end


s[ci][cj] = 0

dequeue = []
dequeue.unshift([ci,cj,0])

ki = [0,1,0,-1] # 右下左上
kj = [1,0,-1,0] # 右下左上
while dequeue.length != 0
  target = dequeue.shift
  x = target[0]
  y = target[1]
  z = target[2]
  # pp target
  # pp dequeue
  sxy = s[x][y]
  if sxy == '.' || sxy == '-'
    s[x][y] = z
  end
  break if (x == di && y == dj)
  4.times do |i|
    xi = x+ki[i]
    yi = y+kj[i]
    if (xi) >= 0 && (xi) < h && (yi) >= 0 && (yi) < w
      next_pos = s[xi][yi] 
      # counter
      next if next_pos == '#'
      if next_pos == '.' || next_pos == '-'
        dequeue.unshift([(xi), (yi), z])
      else
        s[xi][yi] = s[x][y] > s[xi][yi] ? sxy : s[xi][yi]
      end
    end
  end

  [-2,-1,0,1,2].each do |i|
    ss = s[x+i]
    [-2,-1,0,1,2].each do |j|
      if (x + i) >= 0 && (x + i) < h && (y + j) >= 0 && (y + j) < w
        next_pos = ss[y+j]
        if next_pos != '#'
          if next_pos == '.'
            dequeue << [(x+i),(y+j),s[x][y]+1]
            ss[y+j] = '-'
          end
        end
        # counter
      end
    end
  end
  # print_table(s)
end



puts s[di][dj] != "." ? s[di][dj] : "-1"

# puts @count