h, w = list(map(int, input().split()))

ch, cw = [int(x)-1 for x in input().split()]

dh, dw = [int(x)-1 for x in input().split()]

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



from collections import deque

# 座標, 座標, ワープカウント

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

visited = [[-1] * w for _ in range(h)]

visited[ch][cw] = 0



ans = -1

while q:

    i, j, wc = q.popleft()

    if i == dh and j == dw:

        ans = wc

        break

    for mi, mj in ((-1, 0), (1, 0), (0, -1), (0, 1)):

        ni, nj = i + mi, j + mj

        if 0 <= nj < w and 0 <= ni < h:

            if S[ni][nj] == '.' and visited[ni][nj] != wc:

                visited[ni][nj] = wc

                q.appendleft([ni, nj, wc])



    for wi, wj 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)):

        ni, nj = i+wi, j+wj

        if (0 <= nj < w and 0 <= ni < h and S[ni][nj] == '.' and

            visited[ni][nj] == -1):

            visited[ni][nj] = wc+1

            q.append([ni, nj, wc+1])

print(ans)
h, w = list(map(int, input().split()))

ch, cw = [int(x)-1 for x in input().split()]

dh, dw = [int(x)-1 for x in input().split()]

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



from collections import deque

# 座標, 座標, ワープカウント

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

wall = deque()

visited = [[-1] * w for _ in range(h)]

visited[ch][cw] = 0

ans = -2

while True:

    # つながっているエリアを探索

    while q:

        i, j, wc = q.popleft()

        if i == dh and j == dw:

            ans = wc

            break

        next_to_wall = False

        for mi, mj in ((-1, 0), (1, 0), (0, -1), (0, 1)):

            ni, nj = i + mi, j + mj



            if 0 <= nj < w and 0 <= ni < h:

                if S[ni][nj] == '.' and visited[ni][nj] != wc:

                    visited[ni][nj] = wc

                    q.append([ni, nj, wc])

                elif S[ni][nj] == '#':

                    next_to_wall = True

        if next_to_wall:

            wall.append([i, j, wc])

    else:

        # 飛ぶ場所がなかったら終了

        if not wall:

            ans = -1

    if ans > -2:

        break



    while wall:

        i, j, wc = wall.popleft()

        for wi, wj 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)):

            ni, nj = i+wi, j+wj

            if (0 <= nj < w and 0 <= ni < h and S[ni][nj] == '.' and

                (visited[ni][nj] == -1 or visited[ni][nj] > wc+1)):

                visited[ni][nj] = wc+1

                q.append([ni, nj, wc+1])

print(ans)

h, w = list(map(int, input().split()))
ch, cw = [int(x) - 1 for x in input().split()]
dh, dw = [int(x) - 1 for x in input().split()]
S = [list(eval(input())) for _ in range(h)]
from collections import deque

# 座標, 座標, ワープカウント
q = deque([[ch, cw, 0]])
visited = [[-1] * w for _ in range(h)]
visited[ch][cw] = 0
ans = -1
while q:
    i, j, wc = q.popleft()
    if i == dh and j == dw:
        ans = wc
        break
    for mi, mj in ((-1, 0), (1, 0), (0, -1), (0, 1)):
        ni, nj = i + mi, j + mj
        if 0 <= nj < w and 0 <= ni < h:
            if S[ni][nj] == "." and visited[ni][nj] != wc:
                visited[ni][nj] = wc
                q.appendleft([ni, nj, wc])
    for wi, wj 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),
    ):
        ni, nj = i + wi, j + wj
        if 0 <= nj < w and 0 <= ni < h and S[ni][nj] == "." and visited[ni][nj] == -1:
            visited[ni][nj] = wc + 1
            q.append([ni, nj, wc + 1])
print(ans)

h, w = list(map(int, input().split()))
ch, cw = [int(x) - 1 for x in input().split()]
dh, dw = [int(x) - 1 for x in input().split()]
S = [list(eval(input())) for _ in range(h)]
from collections import deque

# 座標, 座標, ワープカウント
q = deque([[ch, cw, 0]])
wall = deque()
visited = [[-1] * w for _ in range(h)]
visited[ch][cw] = 0
ans = -2
while True:
    # つながっているエリアを探索
    while q:
        i, j, wc = q.popleft()
        if i == dh and j == dw:
            ans = wc
            break
        next_to_wall = False
        for mi, mj in ((-1, 0), (1, 0), (0, -1), (0, 1)):
            ni, nj = i + mi, j + mj
            if 0 <= nj < w and 0 <= ni < h:
                if S[ni][nj] == "." and visited[ni][nj] != wc:
                    visited[ni][nj] = wc
                    q.append([ni, nj, wc])
                elif S[ni][nj] == "#":
                    next_to_wall = True
        if next_to_wall:
            wall.append([i, j, wc])
    else:
        # 飛ぶ場所がなかったら終了
        if not wall:
            ans = -1
    if ans > -2:
        break
    while wall:
        i, j, wc = wall.popleft()
        for wi, wj 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),
        ):
            ni, nj = i + wi, j + wj
            if (
                0 <= nj < w
                and 0 <= ni < h
                and S[ni][nj] == "."
                and (visited[ni][nj] == -1 or visited[ni][nj] > wc + 1)
            ):
                visited[ni][nj] = wc + 1
                q.append([ni, nj, wc + 1])
print(ans)

+wall = deque() -ans = -1 -while q: -    i, j, wc = q.popleft() -    if i == dh and j == dw: -        ans = wc +ans = -2 +while True: +    # つながっているエリアを探索 +    while q: +        i, j, wc = q.popleft() +        if i == dh and j == dw: +            ans = wc +            break +        next_to_wall = False +        for mi, mj in ((-1, 0), (1, 0), (0, -1), (0, 1)): +            ni, nj = i + mi, j + mj +            if 0 <= nj < w and 0 <= ni < h: +                if S[ni][nj] == "." and visited[ni][nj] != wc: +                    visited[ni][nj] = wc +                    q.append([ni, nj, wc]) +                elif S[ni][nj] == "#": +                    next_to_wall = True +        if next_to_wall: +            wall.append([i, j, wc]) +    else: +        # 飛ぶ場所がなかったら終了 +        if not wall: +            ans = -1 +    if ans > -2: -    for mi, mj in ((-1, 0), (1, 0), (0, -1), (0, 1)): -        ni, nj = i + mi, j + mj -        if 0 <= nj < w and 0 <= ni < h: -            if S[ni][nj] == "." and visited[ni][nj] != wc: -                visited[ni][nj] = wc -                q.appendleft([ni, nj, wc]) -    for wi, wj 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), -    ): -        ni, nj = i + wi, j + wj -        if 0 <= nj < w and 0 <= ni < h and S[ni][nj] == "." and visited[ni][nj] == -1: -            visited[ni][nj] = wc + 1 -            q.append([ni, nj, wc + 1]) +    while wall: +        i, j, wc = wall.popleft() +        for wi, wj 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), +        ): +            ni, nj = i + wi, j + wj +            if ( +                0 <= nj < w +                and 0 <= ni < h +                and S[ni][nj] == "." +                and (visited[ni][nj] == -1 or visited[ni][nj] > wc + 1) +            ): +                visited[ni][nj] = wc + 1 +                q.append([ni, nj, wc + 1])