

from collections import deque

def resolve():

    drc4 = [(0, 1), (0, -1), (-1, 0), (1, 0)]

    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + drc4]



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

    sy, sx = [int(x) - 1 for x in input().split()]

    gy, gx = [int(x) - 1 for x in input().split()]

    G = [eval(input()) for _ in range(H)]



    INF = 10 ** 12

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



    q = deque()

    q.append((sy, sx, 0))

    ans[sy][sx] = 0



    while q:

        sy, sx, cost = q.popleft()

        for dy, dx in drc4:

            ny = dy + sy

            nx = dx + sx

            if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == "." and ans[ny][nx] > cost:

                ans[ny][nx] = cost

                # q.appendleft 使用

                q.appendleft((ny, nx, cost))



        for dy8, dx8 in warp:

            ny8 = dy8 + sy

            nx8 = dx8 + sx

            if 0 <= ny8 < H and 0 <= nx8 < W and G[ny8][nx8] == "." and ans[ny8][nx8] > cost + 1:

                ans[ny8][nx8] = cost + 1

                # q.appendleft 使用しない

                q.append((ny8, nx8, cost + 1))



    res = ans[gy][gx] if ans[gy][gx] < INF else -1

    print(res)





if __name__ == "__main__":

    resolve()


from collections import deque

def resolve():

    drc4 = [(0, 1), (0, -1), (-1, 0), (1, 0)]

    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + drc4]



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

    sy, sx = [int(x) - 1 for x in input().split()]

    gy, gx = [int(x) - 1 for x in input().split()]

    G = [eval(input()) for _ in range(H)]



    INF = 10 ** 12

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



    q = deque()

    q.append((sy, sx))

    dist[sy][sx] = 0

    cnt = 0

    while True:

        next_warp = []

        while q:

            sy, sx = q.popleft()

            next_warp.append((sy, sx))

            for dy, dx in drc4:

                ny = dy + sy

                nx = dx + sx

                if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == "." and dist[ny][nx] == INF:

                    dist[ny][nx] = cnt

                    q.append((ny, nx))



        for sy, sx in next_warp:

            for dy8, dx8 in warp:

                ny8 = dy8 + sy

                nx8 = dx8 + sx

                if 0 <= ny8 < H and 0 <= nx8 < W and G[ny8][nx8] == "." and dist[ny8][nx8] == INF:

                    dist[ny8][nx8] = cnt + 1

                    q.append((ny8, nx8))



        if not q or dist[gy][gx] != INF:

            break

        cnt += 1



    ans = dist[gy][gx] if dist[gy][gx] != INF else -1

    print(ans)





if __name__ == "__main__":

    resolve()
from collections import deque


def resolve():
    drc4 = [(0, 1), (0, -1), (-1, 0), (1, 0)]
    warp = [
        (i, j)
        for i in range(-2, 3)
        for j in range(-2, 3)
        if (i, j) not in [(0, 0)] + drc4
    ]
    H, W = list(map(int, input().split()))
    sy, sx = [int(x) - 1 for x in input().split()]
    gy, gx = [int(x) - 1 for x in input().split()]
    G = [eval(input()) for _ in range(H)]
    INF = 10**12
    ans = [[INF] * W for _ in range(H)]
    q = deque()
    q.append((sy, sx, 0))
    ans[sy][sx] = 0
    while q:
        sy, sx, cost = q.popleft()
        for dy, dx in drc4:
            ny = dy + sy
            nx = dx + sx
            if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == "." and ans[ny][nx] > cost:
                ans[ny][nx] = cost
                # q.appendleft 使用
                q.appendleft((ny, nx, cost))
        for dy8, dx8 in warp:
            ny8 = dy8 + sy
            nx8 = dx8 + sx
            if (
                0 <= ny8 < H
                and 0 <= nx8 < W
                and G[ny8][nx8] == "."
                and ans[ny8][nx8] > cost + 1
            ):
                ans[ny8][nx8] = cost + 1
                # q.appendleft 使用しない
                q.append((ny8, nx8, cost + 1))
    res = ans[gy][gx] if ans[gy][gx] < INF else -1
    print(res)


if __name__ == "__main__":
    resolve()

from collections import deque


def resolve():
    drc4 = [(0, 1), (0, -1), (-1, 0), (1, 0)]
    warp = [
        (i, j)
        for i in range(-2, 3)
        for j in range(-2, 3)
        if (i, j) not in [(0, 0)] + drc4
    ]
    H, W = list(map(int, input().split()))
    sy, sx = [int(x) - 1 for x in input().split()]
    gy, gx = [int(x) - 1 for x in input().split()]
    G = [eval(input()) for _ in range(H)]
    INF = 10**12
    dist = [[INF] * W for _ in range(H)]
    q = deque()
    q.append((sy, sx))
    dist[sy][sx] = 0
    cnt = 0
    while True:
        next_warp = []
        while q:
            sy, sx = q.popleft()
            next_warp.append((sy, sx))
            for dy, dx in drc4:
                ny = dy + sy
                nx = dx + sx
                if (
                    0 <= ny < H
                    and 0 <= nx < W
                    and G[ny][nx] == "."
                    and dist[ny][nx] == INF
                ):
                    dist[ny][nx] = cnt
                    q.append((ny, nx))
        for sy, sx in next_warp:
            for dy8, dx8 in warp:
                ny8 = dy8 + sy
                nx8 = dx8 + sx
                if (
                    0 <= ny8 < H
                    and 0 <= nx8 < W
                    and G[ny8][nx8] == "."
                    and dist[ny8][nx8] == INF
                ):
                    dist[ny8][nx8] = cnt + 1
                    q.append((ny8, nx8))
        if not q or dist[gy][gx] != INF:
            break
        cnt += 1
    ans = dist[gy][gx] if dist[gy][gx] != INF else -1
    print(ans)


if __name__ == "__main__":
    resolve()

-    ans = [[INF] * W for _ in range(H)] +    dist = [[INF] * W for _ in range(H)] -    q.append((sy, sx, 0)) -    ans[sy][sx] = 0 -    while q: -        sy, sx, cost = q.popleft() -        for dy, dx in drc4: -            ny = dy + sy -            nx = dx + sx -            if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == "." and ans[ny][nx] > cost: -                ans[ny][nx] = cost -                # q.appendleft 使用 -                q.appendleft((ny, nx, cost)) -        for dy8, dx8 in warp: -            ny8 = dy8 + sy -            nx8 = dx8 + sx -            if ( -                0 <= ny8 < H -                and 0 <= nx8 < W -                and G[ny8][nx8] == "." -                and ans[ny8][nx8] > cost + 1 -            ): -                ans[ny8][nx8] = cost + 1 -                # q.appendleft 使用しない -                q.append((ny8, nx8, cost + 1)) -    res = ans[gy][gx] if ans[gy][gx] < INF else -1 -    print(res) +    q.append((sy, sx)) +    dist[sy][sx] = 0 +    cnt = 0 +    while True: +        next_warp = [] +        while q: +            sy, sx = q.popleft() +            next_warp.append((sy, sx)) +            for dy, dx in drc4: +                ny = dy + sy +                nx = dx + sx +                if ( +                    0 <= ny < H +                    and 0 <= nx < W +                    and G[ny][nx] == "." +                    and dist[ny][nx] == INF +                ): +                    dist[ny][nx] = cnt +                    q.append((ny, nx)) +        for sy, sx in next_warp: +            for dy8, dx8 in warp: +                ny8 = dy8 + sy +                nx8 = dx8 + sx +                if ( +                    0 <= ny8 < H +                    and 0 <= nx8 < W +                    and G[ny8][nx8] == "." +                    and dist[ny8][nx8] == INF +                ): +                    dist[ny8][nx8] = cnt + 1 +                    q.append((ny8, nx8)) +        if not q or dist[gy][gx] != INF: +            break +        cnt += 1 +    ans = dist[gy][gx] if dist[gy][gx] != INF else -1 +    print(ans)