# ABC176 D Wizard 

# 地図系。ワープ有　01BFS??



from collections import deque

H,W = list(map(int,input().split()))

sx,sy = list(map(int,input().split()))

gx,gy = list(map(int,input().split()))

sx,sy,gx,gy = sx-1,sy-1,gx-1,gy-1



C=[list(eval(input())) for i in range(H)]

inf = float("inf")



for i in range(H):

    for j in range(W):

        if C[i][j] == ".":

            C[i][j] =0

        else:

            C[i][j]=1



d0=[[1,0],[0,1],[-1,0],[0,-1]]

d1=[[-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]]



dist = [[inf]*W for i in range(H)]

dist[sx][sy]=0

que=deque([[0,sx,sy]])



while que:

    L,x,y = que.popleft()

    for dx,dy in d0:

        nx,ny=x+dx,y+dy

        if 0<=nx<H and 0<=ny<W and L+C[nx][ny] < dist[nx][ny] and C[nx][ny]==0:

            dist[nx][ny] = L+C[nx][ny]

            que.appendleft([L,nx,ny])

            

    for dx,dy in d1:

        nx,ny=x+dx,y+dy

        if 0<=nx<H and 0<=ny<W and L+C[nx][ny]+1 < dist[nx][ny] and C[nx][ny]==0:

            dist[nx][ny] = L+C[nx][ny]+1

            que.append([L+1,nx,ny])

ans=dist[gx][gy]

if ans==inf:print((-1))

else: print(ans)
# ABC176 D Wizard 

# 地図系。ワープ有　01BFS??



from collections import deque

H,W = list(map(int,input().split()))

sx,sy = list(map(int,input().split()))

gx,gy = list(map(int,input().split()))

sx,sy,gx,gy = sx-1,sy-1,gx-1,gy-1



C=[list(eval(input())) for i in range(H)]

inf = float("inf")



for i in range(H):

    for j in range(W):

        if C[i][j] == ".":

            C[i][j] =0

        else:

            C[i][j]=1



d0=[[1,0],[0,1],[-1,0],[0,-1]]

d1=[[-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]]



dist = [[inf]*W for i in range(H)]

dist[sx][sy]=0

que=deque([[0,sx,sy]])



while que:

    L,x,y = que.popleft()

    for dx,dy in d0:

        nx,ny=x+dx,y+dy

        if 0<=nx<H and 0<=ny<W and L < dist[nx][ny] and C[nx][ny]==0:

            dist[nx][ny] = L

            que.appendleft([L,nx,ny])

            

    for dx,dy in d1:

        nx,ny=x+dx,y+dy

        if 0<=nx<H and 0<=ny<W and L+1 < dist[nx][ny] and C[nx][ny]==0:

            dist[nx][ny] = L+1

            que.append([L+1,nx,ny])

ans=dist[gx][gy]

if ans==inf:print((-1))

else: print(ans)
# ABC176 D Wizard
# 地図系。ワープ有　01BFS??
from collections import deque

H, W = list(map(int, input().split()))
sx, sy = list(map(int, input().split()))
gx, gy = list(map(int, input().split()))
sx, sy, gx, gy = sx - 1, sy - 1, gx - 1, gy - 1
C = [list(eval(input())) for i in range(H)]
inf = float("inf")
for i in range(H):
    for j in range(W):
        if C[i][j] == ".":
            C[i][j] = 0
        else:
            C[i][j] = 1
d0 = [[1, 0], [0, 1], [-1, 0], [0, -1]]
d1 = [
    [-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],
]
dist = [[inf] * W for i in range(H)]
dist[sx][sy] = 0
que = deque([[0, sx, sy]])
while que:
    L, x, y = que.popleft()
    for dx, dy in d0:
        nx, ny = x + dx, y + dy
        if (
            0 <= nx < H
            and 0 <= ny < W
            and L + C[nx][ny] < dist[nx][ny]
            and C[nx][ny] == 0
        ):
            dist[nx][ny] = L + C[nx][ny]
            que.appendleft([L, nx, ny])
    for dx, dy in d1:
        nx, ny = x + dx, y + dy
        if (
            0 <= nx < H
            and 0 <= ny < W
            and L + C[nx][ny] + 1 < dist[nx][ny]
            and C[nx][ny] == 0
        ):
            dist[nx][ny] = L + C[nx][ny] + 1
            que.append([L + 1, nx, ny])
ans = dist[gx][gy]
if ans == inf:
    print((-1))
else:
    print(ans)

# ABC176 D Wizard
# 地図系。ワープ有　01BFS??
from collections import deque

H, W = list(map(int, input().split()))
sx, sy = list(map(int, input().split()))
gx, gy = list(map(int, input().split()))
sx, sy, gx, gy = sx - 1, sy - 1, gx - 1, gy - 1
C = [list(eval(input())) for i in range(H)]
inf = float("inf")
for i in range(H):
    for j in range(W):
        if C[i][j] == ".":
            C[i][j] = 0
        else:
            C[i][j] = 1
d0 = [[1, 0], [0, 1], [-1, 0], [0, -1]]
d1 = [
    [-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],
]
dist = [[inf] * W for i in range(H)]
dist[sx][sy] = 0
que = deque([[0, sx, sy]])
while que:
    L, x, y = que.popleft()
    for dx, dy in d0:
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W and L < dist[nx][ny] and C[nx][ny] == 0:
            dist[nx][ny] = L
            que.appendleft([L, nx, ny])
    for dx, dy in d1:
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W and L + 1 < dist[nx][ny] and C[nx][ny] == 0:
            dist[nx][ny] = L + 1
            que.append([L + 1, nx, ny])
ans = dist[gx][gy]
if ans == inf:
    print((-1))
else:
    print(ans)

-        if ( -            0 <= nx < H -            and 0 <= ny < W -            and L + C[nx][ny] < dist[nx][ny] -            and C[nx][ny] == 0 -        ): -            dist[nx][ny] = L + C[nx][ny] +        if 0 <= nx < H and 0 <= ny < W and L < dist[nx][ny] and C[nx][ny] == 0: +            dist[nx][ny] = L -        if ( -            0 <= nx < H -            and 0 <= ny < W -            and L + C[nx][ny] + 1 < dist[nx][ny] -            and C[nx][ny] == 0 -        ): -            dist[nx][ny] = L + C[nx][ny] + 1 +        if 0 <= nx < H and 0 <= ny < W and L + 1 < dist[nx][ny] and C[nx][ny] == 0: +            dist[nx][ny] = L + 1