from sys import stdin

nii=lambda:list(map(int,stdin.readline().split()))

lnii=lambda:list(map(int,stdin.readline().split()))

from collections import deque



h,w=nii()

ch,cw=nii()

dh,dw=nii()

s=[list(eval(input())) for i in range(h)]



ch-=1

cw-=1

dh-=1

dw-=1



ans=[[-1 for i in range(w)] for i in range(h)]

ans[ch][cw]=0



que=deque()

que.append((ch,cw))



while que:

  y,x=que.popleft()



  if y==dh and x==dw:

    print((ans[y][x]))

    exit()



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

    ny=y+dy

    nx=x+dx

    if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#':

      que.appendleft((ny,nx))

      s[ny][nx]='#'

      ans[ny][nx]=ans[y][x]



    for i in range(-2,3):

      for j in range(-2,3):

        ny=y+i

        nx=x+j

        if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#' and ans[ny][nx]==-1:

          que.append((ny,nx))

          ans[ny][nx]=ans[y][x]+1



print((-1))
from sys import stdin

nii=lambda:list(map(int,stdin.readline().split()))

lnii=lambda:list(map(int,stdin.readline().split()))

from collections import deque



h,w=nii()

ch,cw=nii()

ch-=1

cw-=1

dh,dw=nii()

dh-=1

dw-=1

s=[list(eval(input())) for i in range(h)]



dist=[[-1]*w for i in range(h)]



def BFS(que):

  while que:

    y,x=que.popleft()



    if y==dh and x==dw:

      print((dist[y][x]))

      exit()



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

      ny=y+dy

      nx=x+dx

      if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#':

        que.appendleft((ny,nx))

        s[ny][nx]='#'

        dist[ny][nx]=dist[y][x]

    for dy,dx in [[-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],\

                  [-1,-2],[-1,-1],[-1,1],[-1,2],\

                  [0,-2],[0,2],\

                  [1,-2],[1,-1],[1,1],[1,2],\

                  [2,-2],[2,-1],[2,0],[2,1],[2,2]]:

      ny=y+dy

      nx=x+dx

      if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#' and dist[ny][nx]==-1:

        que.append((ny,nx))

        dist[ny][nx]=dist[y][x]+1



dist[ch][cw]=0



que=deque()

que.append((ch,cw))



BFS(que)



print((-1))
from sys import stdin

nii = lambda: list(map(int, stdin.readline().split()))
lnii = lambda: list(map(int, stdin.readline().split()))
from collections import deque

h, w = nii()
ch, cw = nii()
dh, dw = nii()
s = [list(eval(input())) for i in range(h)]
ch -= 1
cw -= 1
dh -= 1
dw -= 1
ans = [[-1 for i in range(w)] for i in range(h)]
ans[ch][cw] = 0
que = deque()
que.append((ch, cw))
while que:
    y, x = que.popleft()
    if y == dh and x == dw:
        print((ans[y][x]))
        exit()
    for dy, dx in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        ny = y + dy
        nx = x + dx
        if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#":
            que.appendleft((ny, nx))
            s[ny][nx] = "#"
            ans[ny][nx] = ans[y][x]
        for i in range(-2, 3):
            for j in range(-2, 3):
                ny = y + i
                nx = x + j
                if (
                    0 <= ny < h
                    and 0 <= nx < w
                    and s[ny][nx] != "#"
                    and ans[ny][nx] == -1
                ):
                    que.append((ny, nx))
                    ans[ny][nx] = ans[y][x] + 1
print((-1))

from sys import stdin

nii = lambda: list(map(int, stdin.readline().split()))
lnii = lambda: list(map(int, stdin.readline().split()))
from collections import deque

h, w = nii()
ch, cw = nii()
ch -= 1
cw -= 1
dh, dw = nii()
dh -= 1
dw -= 1
s = [list(eval(input())) for i in range(h)]
dist = [[-1] * w for i in range(h)]


def BFS(que):
    while que:
        y, x = que.popleft()
        if y == dh and x == dw:
            print((dist[y][x]))
            exit()
        for dy, dx in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#":
                que.appendleft((ny, nx))
                s[ny][nx] = "#"
                dist[ny][nx] = dist[y][x]
        for dy, dx in [
            [-2, -2],
            [-2, -1],
            [-2, 0],
            [-2, 1],
            [-2, 2],
            [-1, -2],
            [-1, -1],
            [-1, 1],
            [-1, 2],
            [0, -2],
            [0, 2],
            [1, -2],
            [1, -1],
            [1, 1],
            [1, 2],
            [2, -2],
            [2, -1],
            [2, 0],
            [2, 1],
            [2, 2],
        ]:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#" and dist[ny][nx] == -1:
                que.append((ny, nx))
                dist[ny][nx] = dist[y][x] + 1


dist[ch][cw] = 0
que = deque()
que.append((ch, cw))
BFS(que)
print((-1))

-dh, dw = nii() -s = [list(eval(input())) for i in range(h)] +dh, dw = nii() -ans = [[-1 for i in range(w)] for i in range(h)] -ans[ch][cw] = 0 +s = [list(eval(input())) for i in range(h)] +dist = [[-1] * w for i in range(h)] + + +def BFS(que): +    while que: +        y, x = que.popleft() +        if y == dh and x == dw: +            print((dist[y][x])) +            exit() +        for dy, dx in [[1, 0], [-1, 0], [0, 1], [0, -1]]: +            ny = y + dy +            nx = x + dx +            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#": +                que.appendleft((ny, nx)) +                s[ny][nx] = "#" +                dist[ny][nx] = dist[y][x] +        for dy, dx in [ +            [-2, -2], +            [-2, -1], +            [-2, 0], +            [-2, 1], +            [-2, 2], +            [-1, -2], +            [-1, -1], +            [-1, 1], +            [-1, 2], +            [0, -2], +            [0, 2], +            [1, -2], +            [1, -1], +            [1, 1], +            [1, 2], +            [2, -2], +            [2, -1], +            [2, 0], +            [2, 1], +            [2, 2], +        ]: +            ny = y + dy +            nx = x + dx +            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#" and dist[ny][nx] == -1: +                que.append((ny, nx)) +                dist[ny][nx] = dist[y][x] + 1 + + +dist[ch][cw] = 0 -while que: -    y, x = que.popleft() -    if y == dh and x == dw: -        print((ans[y][x])) -        exit() -    for dy, dx in [[-1, 0], [1, 0], [0, -1], [0, 1]]: -        ny = y + dy -        nx = x + dx -        if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#": -            que.appendleft((ny, nx)) -            s[ny][nx] = "#" -            ans[ny][nx] = ans[y][x] -        for i in range(-2, 3): -            for j in range(-2, 3): -                ny = y + i -                nx = x + j -                if ( -                    0 <= ny < h -                    and 0 <= nx < w -                    and s[ny][nx] != "#" -                    and ans[ny][nx] == -1 -                ): -                    que.append((ny, nx)) -                    ans[ny][nx] = ans[y][x] + 1 +BFS(que)