INF=10**6

f=lambda:list(map(int,input().split()))

h,w=f()

ch,cw=f()

dh,dw=f()

ch-=1

cw-=1

dh-=1

dw-=1

g=[eval(input()) for _ in range(h)]

from collections import *

d=[[INF]*w for _ in range(h)]

q=deque([(0,ch,cw)])

while q:

  td,tx,ty=q.popleft()

  if d[tx][ty]<=td: continue

  d[tx][ty]=td

  for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:

    nx,ny=tx+dx,ty+dy

    if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.':

      q.appendleft((td,nx,ny))

  for dx in range(-2,3):

    for dy in range(-2,3):

      nx,ny=tx+dx,ty+dy

      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.':

        q.append((td+1,nx,ny))

a=d[dh][dw]

print(([-1,a][a<INF]))
INF=10**6

f=lambda:list(map(int,input().split()))

h,w=f()

ch,cw=f()

dh,dw=f()

g=[eval(input()) for _ in range(h)]

d=[[INF]*w for _ in range(h)]

from collections import *

q=deque([(0,ch-1,cw-1)])

while q:

  c,x,y=q.popleft()

  if d[x][y]<=c: continue

  d[x][y]=c

  for dx in range(-2,3):

    for dy in range(-2,3):

      j=abs(dx)+abs(dy)

      nx,ny=x+dx,y+dy

      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and j:

        if j>1: q.append((c+1,nx,ny))

        else: q.appendleft((c,nx,ny))

a=d[dh-1][dw-1]

print(([-1,a][a<INF]))
INF = 10**6
f = lambda: list(map(int, input().split()))
h, w = f()
ch, cw = f()
dh, dw = f()
ch -= 1
cw -= 1
dh -= 1
dw -= 1
g = [eval(input()) for _ in range(h)]
from collections import *

d = [[INF] * w for _ in range(h)]
q = deque([(0, ch, cw)])
while q:
    td, tx, ty = q.popleft()
    if d[tx][ty] <= td:
        continue
    d[tx][ty] = td
    for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        nx, ny = tx + dx, ty + dy
        if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == ".":
            q.appendleft((td, nx, ny))
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            nx, ny = tx + dx, ty + dy
            if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == ".":
                q.append((td + 1, nx, ny))
a = d[dh][dw]
print(([-1, a][a < INF]))

INF = 10**6
f = lambda: list(map(int, input().split()))
h, w = f()
ch, cw = f()
dh, dw = f()
g = [eval(input()) for _ in range(h)]
d = [[INF] * w for _ in range(h)]
from collections import *

q = deque([(0, ch - 1, cw - 1)])
while q:
    c, x, y = q.popleft()
    if d[x][y] <= c:
        continue
    d[x][y] = c
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            j = abs(dx) + abs(dy)
            nx, ny = x + dx, y + dy
            if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == "." and j:
                if j > 1:
                    q.append((c + 1, nx, ny))
                else:
                    q.appendleft((c, nx, ny))
a = d[dh - 1][dw - 1]
print(([-1, a][a < INF]))

-ch -= 1 -cw -= 1 -dh -= 1 -dw -= 1 +d = [[INF] * w for _ in range(h)] -d = [[INF] * w for _ in range(h)] -q = deque([(0, ch, cw)]) +q = deque([(0, ch - 1, cw - 1)]) -    td, tx, ty = q.popleft() -    if d[tx][ty] <= td: +    c, x, y = q.popleft() +    if d[x][y] <= c: -    d[tx][ty] = td -    for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]: -        nx, ny = tx + dx, ty + dy -        if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == ".": -            q.appendleft((td, nx, ny)) +    d[x][y] = c -            nx, ny = tx + dx, ty + dy -            if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == ".": -                q.append((td + 1, nx, ny)) -a = d[dh][dw] +            j = abs(dx) + abs(dy) +            nx, ny = x + dx, y + dy +            if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == "." and j: +                if j > 1: +                    q.append((c + 1, nx, ny)) +                else: +                    q.appendleft((c, nx, ny)) +a = d[dh - 1][dw - 1]