import sys

from collections import deque

input = lambda: sys.stdin.readline().rstrip()



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



ch, cw = list(map(int, input().split()))

ch -= 1

cw -= 1

dh, dw = list(map(int, input().split()))

dh -= 1

dw -= 1



maze = []

for i in range(H):

    a = list(eval(input()))

    maze.append(a)



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



dist = [[float("inf") for _ in range(W)] for _ in range(H)]

dist[ch][cw] = 0



while queue:

    y, x, cnt= queue.popleft()



    for a, b in ([1, 0], [-1, 0], [0, 1], [0, -1]):

        new_y, new_x = y+a, x+b

        if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != '#':

            if dist[new_y][new_x] == float("inf") or dist[new_y][new_x] > cnt:

                queue.appendleft((new_y, new_x, cnt))

                dist[new_y][new_x] = cnt



    for a in range(-2, 3):

        for b in range(-2, 3):

            new_y, new_x = y + a, x + b

            if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != '#':

                if dist[new_y][new_x] == float("inf") or dist[new_y][new_x] > cnt + 1:

                    queue.append((new_y, new_x, cnt+1))

                    dist[new_y][new_x] = cnt + 1



print((-1 if dist[dh][dw] == float("inf") else dist[dh][dw]))
from collections import deque

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



ch, cw = list(map(int, input().split()))



dh, dw = list(map(int, input().split()))



ch -= 1

cw -= 1

dh -= 1

dw -= 1



maze = []

for i in range(H):

    h = list(eval(input()))

    maze.append(h)



dist = [[float("inf") for _ in range(W)] for _ in range(H)]

dist[ch][cw] = 0



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



while queue:

    y, x, d = queue.popleft()



    if y == dh and x == dw:

        print(d)

        exit()



    for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):

        new_y, new_x = y + i, x + j

        if 0 <= new_y < H and 0 <= new_x < W:

            if maze[new_y][new_x] != '#':

                if dist[new_y][new_x] > d:

                    dist[new_y][new_x] = d

                    queue.appendleft((new_y, new_x, d))



    for i in range(-2, 3):

        for j in range(-2, 3):

            new_y, new_x = y + i, x + j

            if 0 <= new_y < H and 0 <= new_x < W:

                if maze[new_y][new_x] != '#':

                    if dist[new_y][new_x] > d+1:

                        dist[new_y][new_x] = d+1

                        queue.append((new_y, new_x, d+1))



print((-1))
import sys
from collections import deque

input = lambda: sys.stdin.readline().rstrip()
H, W = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
ch -= 1
cw -= 1
dh, dw = list(map(int, input().split()))
dh -= 1
dw -= 1
maze = []
for i in range(H):
    a = list(eval(input()))
    maze.append(a)
queue = deque([(ch, cw, 0)])
dist = [[float("inf") for _ in range(W)] for _ in range(H)]
dist[ch][cw] = 0
while queue:
    y, x, cnt = queue.popleft()
    for a, b in ([1, 0], [-1, 0], [0, 1], [0, -1]):
        new_y, new_x = y + a, x + b
        if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != "#":
            if dist[new_y][new_x] == float("inf") or dist[new_y][new_x] > cnt:
                queue.appendleft((new_y, new_x, cnt))
                dist[new_y][new_x] = cnt
    for a in range(-2, 3):
        for b in range(-2, 3):
            new_y, new_x = y + a, x + b
            if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != "#":
                if dist[new_y][new_x] == float("inf") or dist[new_y][new_x] > cnt + 1:
                    queue.append((new_y, new_x, cnt + 1))
                    dist[new_y][new_x] = cnt + 1
print((-1 if dist[dh][dw] == float("inf") else dist[dh][dw]))

from collections import deque

H, W = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
ch -= 1
cw -= 1
dh -= 1
dw -= 1
maze = []
for i in range(H):
    h = list(eval(input()))
    maze.append(h)
dist = [[float("inf") for _ in range(W)] for _ in range(H)]
dist[ch][cw] = 0
queue = deque([(ch, cw, 0)])
while queue:
    y, x, d = queue.popleft()
    if y == dh and x == dw:
        print(d)
        exit()
    for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
        new_y, new_x = y + i, x + j
        if 0 <= new_y < H and 0 <= new_x < W:
            if maze[new_y][new_x] != "#":
                if dist[new_y][new_x] > d:
                    dist[new_y][new_x] = d
                    queue.appendleft((new_y, new_x, d))
    for i in range(-2, 3):
        for j in range(-2, 3):
            new_y, new_x = y + i, x + j
            if 0 <= new_y < H and 0 <= new_x < W:
                if maze[new_y][new_x] != "#":
                    if dist[new_y][new_x] > d + 1:
                        dist[new_y][new_x] = d + 1
                        queue.append((new_y, new_x, d + 1))
print((-1))

-import sys -input = lambda: sys.stdin.readline().rstrip() +dh, dw = list(map(int, input().split())) -dh, dw = list(map(int, input().split())) -    a = list(eval(input())) -    maze.append(a) -queue = deque([(ch, cw, 0)]) +    h = list(eval(input())) +    maze.append(h) +queue = deque([(ch, cw, 0)]) -    y, x, cnt = queue.popleft() -    for a, b in ([1, 0], [-1, 0], [0, 1], [0, -1]): -        new_y, new_x = y + a, x + b -        if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != "#": -            if dist[new_y][new_x] == float("inf") or dist[new_y][new_x] > cnt: -                queue.appendleft((new_y, new_x, cnt)) -                dist[new_y][new_x] = cnt -    for a in range(-2, 3): -        for b in range(-2, 3): -            new_y, new_x = y + a, x + b -            if 0 <= new_y < H and 0 <= new_x < W and maze[new_y][new_x] != "#": -                if dist[new_y][new_x] == float("inf") or dist[new_y][new_x] > cnt + 1: -                    queue.append((new_y, new_x, cnt + 1)) -                    dist[new_y][new_x] = cnt + 1 -print((-1 if dist[dh][dw] == float("inf") else dist[dh][dw])) +    y, x, d = queue.popleft() +    if y == dh and x == dw: +        print(d) +        exit() +    for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]): +        new_y, new_x = y + i, x + j +        if 0 <= new_y < H and 0 <= new_x < W: +            if maze[new_y][new_x] != "#": +                if dist[new_y][new_x] > d: +                    dist[new_y][new_x] = d +                    queue.appendleft((new_y, new_x, d)) +    for i in range(-2, 3): +        for j in range(-2, 3): +            new_y, new_x = y + i, x + j +            if 0 <= new_y < H and 0 <= new_x < W: +                if maze[new_y][new_x] != "#": +                    if dist[new_y][new_x] > d + 1: +                        dist[new_y][new_x] = d + 1 +                        queue.append((new_y, new_x, d + 1)) +print((-1))