output_description: Print the minimum number of times the magician needs to use the magic. If he
  cannot reach (D_h,D_w), print `-1` instead.
  
  * * *
input_description: 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}
attempt: from heapq import heappush, heappop, heapify
  
  rep = range
  R = range
  
  
  def Golf():
      n, *t = map(int, open(0).read().split())
  
  
  def I():
      return int(input())
  
  
  def S_():
      return input()
  
  
  def IS():
      return input().split()
  
  
  def LS():
      return [i for i in input().split()]
  
  
  def MI():
      return map(int, input().split())
  
  
  def LI():
      return [int(i) for i in input().split()]
  
  
  def LI_():
      return [int(i) - 1 for i in input().split()]
  
  
  def NI(n):
      return [int(input()) for i in range(n)]
  
  
  def NI_(n):
      return [int(input()) - 1 for i in range(n)]
  
  
  def StoLI():
      return [ord(i) - 97 for i in input()]
  
  
  def ItoS(n):
      return chr(n + 97)
  
  
  def LtoS(ls):
      return "".join([chr(i + 97) for i in ls])
  
  
  def GI(V, E, ls=None, Directed=False, index=1):
      org_inp = []
      g = [[] for i in range(V)]
      FromStdin = True if ls == None else False
      for i in range(E):
          if FromStdin:
              inp = LI()
              org_inp.append(inp)
          else:
              inp = ls[i]
          if len(inp) == 2:
              a, b = inp
              c = 1
          else:
              a, b, c = inp
          if index == 1:
              a -= 1
              b -= 1
          aa = (a, c)
          bb = (b, c)
          g[a].append(bb)
          if not Directed:
              g[b].append(aa)
      return g, org_inp
  
  
  def GGI(
      h, w, search=None, replacement_of_found=".", mp_def={"#": 1, ".": 0}, boundary=1
  ):
      # h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
      mp = [boundary] * (w + 2)
      found = {}
      for i in R(h):
          s = input()
          for char in search:
              if char in s:
                  found[char] = (i + 1) * (w + 2) + s.index(char) + 1
                  mp_def[char] = mp_def[replacement_of_found]
          mp += [boundary] + [mp_def[j] for j in s] + [boundary]
      mp += [boundary] * (w + 2)
      return h + 2, w + 2, mp, found
  
  
  mo = 10**9 + 7
  FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
  EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
  compas = dict(zip("WENS", FourNb))
  cursol = dict(zip("LRUD", FourNb))
  import sys
  
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  input = lambda: sys.stdin.readline().rstrip()
  
  inf = 1 << 20
  
  
  def dijkstra(edge, st):
      n = len(edge)
      d = [(0 if st == i else inf) for i in range(n)]
      q = [(0, st)]
      heapify(q)
      v = [False] * n
      while q:
          dist, cur = heappop(q)
          if v[cur]:
              continue
          v[cur] = True
          for x in edge[cur]:
              dst, dist = x >> 1, x % 2
              alt = d[cur] + dist
              if alt < d[dst]:
                  d[dst] = alt
                  heappush(q, (alt, dst))
      return d
  
  
  show_flg = False
  show_flg = True
  
  ans = 0
  
  h, w = LI()
  sh, sw = LI()
  gh, gw = LI()
  
  w += 2
  m = [1] * w
  for i in range(h):
      s = input()
      m += [1] + [i == "#" for i in s] + [1]
  m += [1] * w
  h += 2
  n = h * w
  v = [-1] * w * h
  
  
  nbw = [
      -2 * w + 1,
      -2 * w + 2,
      -w + 1,
      -w + 2,
      2,
      2 * w + 1,
      2 * w + 2,
      w + 1,
      w + 2,
      +2 * w,
      -2 * w,
      -2 * w - 1,
      -2 * w - 2,
      -w - 1,
      -w - 2,
      -2,
      2 * w - 1,
      2 * w - 2,
      w - 1,
      w - 2,
  ]
  nbw = [
      2,
      2 * w + 1,
      2 * w + 2,
      w + 1,
      w + 2,
      +2 * w,
      2 * w - 1,
      2 * w - 2,
      w - 1,
      w - 2,
  ]
  nb = [1, -1, w, -w]
  nb = [1, w]
  d = [-1] * n
  g = [[] for i in range(n)]
  for i in range(n):
      if m[i] == 1:
          continue
      for di in nbw:
          nx = i + di
          if 0 <= nx < n and m[nx] != 1:
              g[i] += ((nx * 2 + 1),)
              g[nx] += ((i * 2 + 1),)
      for di in nb:
          nx = i + di
          if 0 <= nx < n and m[nx] != 1:
              g[i] += ((nx * 2),)
              g[nx] += ((i * 2),)
  
  d = dijkstra(g, sh * w + sw)
  ans = d[gh * w + gw]
  if ans == inf:
      ans = -1
  print(ans)
  
problem_description: 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)?
samples: [{"input": "4 4\n    1 1\n    4 4\n    ..#.\n    ..#.\n    .#..\n    .#..", "output": "1\n    \n\nFor example, by walking to (2,2) and then using the magic to travel to (4,4),\njust one use of magic is enough.\n\nNote that he cannot walk diagonally.\n\n* * *"}, {"input": "4 4\n    1 4\n    4 1\n    .##.\n    ####\n    ####\n    .##.", "output": "-1\n    \n\nHe cannot move from there.\n\n* * *"}, {"input": "4 4\n    2 2\n    3 3\n    ....\n    ....\n    ....\n    ....", "output": "0\n    \n\nNo use of magic is needed.\n\n* * *"}, {"input": "4 5\n    1 2\n    2 5\n    #.###\n    ####.\n    #..##\n    #..##", "output": "2"}]