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

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

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

sy,sx = sy-1,sx-1

gy,gx = gy-1,gx-1

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



import heapq

INF = float('inf')

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

dist[sy][sx] = 0

hq = [(0,sx+sy*W)]

heapq.heapify(hq)

while hq:

    d,v = heapq.heappop(hq)

    y,x = divmod(v,W)

    for dx in range(-2,3):

        for dy in range(-2,3):

            if dx == dy == 0: continue

            nx,ny = x+dx,y+dy

            if not 0 <= nx < W: continue

            if not 0 <= ny < H: continue

            if S[ny][nx] == '#': continue

            if abs(dx)+abs(dy) <= 1:

                if dist[ny][nx] <= dist[y][x]: continue

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

                heapq.heappush(hq, (d,nx+ny*W))

            else:

                if dist[ny][nx] <= dist[y][x] + 1: continue

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

                heapq.heappush(hq, (d+1, nx+ny*W))

print((-1 if dist[gy][gx]==INF else dist[gy][gx]))
H,W = list(map(int,input().split()))

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

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

sy,sx = sy-1,sx-1

gy,gx = gy-1,gx-1

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



from collections import deque

INF = float('inf')

dist = [[INF]*W for _ in range(H)]

dist[sy][sx] = 0

q = deque([(sx,sy)])

while q:

    x,y = q.popleft()

    for dx in range(-2,3):

        for dy in range(-2,3):

            if dx == dy == 0: continue

            nx,ny = x+dx,y+dy

            if not 0 <= nx < W: continue

            if not 0 <= ny < H: continue

            if S[ny][nx] == '#': continue

            if abs(dx)+abs(dy) <= 1:

                if dist[ny][nx] <= dist[y][x]: continue

                q.appendleft((nx,ny))

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

            else:

                if dist[ny][nx] <= dist[y][x] + 1: continue

                q.append((nx,ny))

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



print((-1 if dist[gy][gx]==INF else dist[gy][gx]))
H, W = list(map(int, input().split()))
sy, sx = list(map(int, input().split()))
gy, gx = list(map(int, input().split()))
sy, sx = sy - 1, sx - 1
gy, gx = gy - 1, gx - 1
S = [eval(input()) for i in range(H)]
import heapq

INF = float("inf")
dist = [[INF] * W for i in range(H)]
dist[sy][sx] = 0
hq = [(0, sx + sy * W)]
heapq.heapify(hq)
while hq:
    d, v = heapq.heappop(hq)
    y, x = divmod(v, W)
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            if dx == dy == 0:
                continue
            nx, ny = x + dx, y + dy
            if not 0 <= nx < W:
                continue
            if not 0 <= ny < H:
                continue
            if S[ny][nx] == "#":
                continue
            if abs(dx) + abs(dy) <= 1:
                if dist[ny][nx] <= dist[y][x]:
                    continue
                dist[ny][nx] = dist[y][x]
                heapq.heappush(hq, (d, nx + ny * W))
            else:
                if dist[ny][nx] <= dist[y][x] + 1:
                    continue
                dist[ny][nx] = dist[y][x] + 1
                heapq.heappush(hq, (d + 1, nx + ny * W))
print((-1 if dist[gy][gx] == INF else dist[gy][gx]))

H, W = list(map(int, input().split()))
sy, sx = list(map(int, input().split()))
gy, gx = list(map(int, input().split()))
sy, sx = sy - 1, sx - 1
gy, gx = gy - 1, gx - 1
S = [eval(input()) for i in range(H)]
from collections import deque

INF = float("inf")
dist = [[INF] * W for _ in range(H)]
dist[sy][sx] = 0
q = deque([(sx, sy)])
while q:
    x, y = q.popleft()
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            if dx == dy == 0:
                continue
            nx, ny = x + dx, y + dy
            if not 0 <= nx < W:
                continue
            if not 0 <= ny < H:
                continue
            if S[ny][nx] == "#":
                continue
            if abs(dx) + abs(dy) <= 1:
                if dist[ny][nx] <= dist[y][x]:
                    continue
                q.appendleft((nx, ny))
                dist[ny][nx] = dist[y][x]
            else:
                if dist[ny][nx] <= dist[y][x] + 1:
                    continue
                q.append((nx, ny))
                dist[ny][nx] = dist[y][x] + 1
print((-1 if dist[gy][gx] == INF else dist[gy][gx]))

-import heapq +from collections import deque -dist = [[INF] * W for i in range(H)] +dist = [[INF] * W for _ in range(H)] -hq = [(0, sx + sy * W)] -heapq.heapify(hq) -while hq: -    d, v = heapq.heappop(hq) -    y, x = divmod(v, W) +q = deque([(sx, sy)]) +while q: +    x, y = q.popleft() +                q.appendleft((nx, ny)) -                heapq.heappush(hq, (d, nx + ny * W)) +                q.append((nx, ny)) -                heapq.heappush(hq, (d + 1, nx + ny * W))