INF = 10**9 + 7





def main():

    from heapq import heappop, heappush

    H, W = (int(i) for i in input().split())

    Ch, Cw = (int(i)-1 for i in input().split())

    Dh, Dw = (int(i)-1 for i in input().split())

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



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

    dist[Ch][Cw] = 0

    que = [(0, Ch*W + Cw)]

    while que:

        u_dist, u = heappop(que)

        uh, uw = u//W, u % W

        if dist[uh][uw] < u_dist:

            continue

        for dh in range(-2, 3):

            for dw in range(-2, 3):

                nh = uh + dh

                nw = uw + dw

                if nh < 0 or H <= nh or nw < 0 or W <= nw:

                    continue

                if A[nh][nw] == "#":

                    continue

                cost = 0 if abs(dh) + abs(dw) <= 1 else 1

                if dist[nh][nw] > dist[uh][uw] + cost:

                    dist[nh][nw] = dist[uh][uw] + cost

                    heappush(que, (dist[nh][nw], nh*W + nw))



    if dist[Dh][Dw] == INF:

        print((-1))

    else:

        print((dist[Dh][Dw]))



    # for h in range(H):

    #     print(dist[h])





if __name__ == '__main__':

    main()

# 01BFS

INF = 10**9 + 7





def main():

    from collections import deque

    H, W = (int(i) for i in input().split())

    Ch, Cw = (int(i)-1 for i in input().split())

    Dh, Dw = (int(i)-1 for i in input().split())

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



    def bfs_2d(c, sy, sx, gh, gw, H, W):

        que = deque()

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



        dist[sy][sx] = 0

        que.append((sy, sx))

        while que:

            uh, uw = que.popleft()

            for dh in range(-2, 3):

                for dw in range(-2, 3):

                    next_h = uh + dh

                    next_w = uw + dw

                    cost = 0 if abs(dh) + abs(dw) <= 1 else 1

                    if not(0 <= next_h < H and 0 <= next_w < W):

                        continue

                    if c[next_h][next_w] == "#":

                        continue

                    if dist[next_h][next_w] > dist[uh][uw] + cost:

                        dist[next_h][next_w] = dist[uh][uw] + cost

                        if cost == 1:

                            que.append((next_h, next_w))

                        else:

                            que.appendleft((next_h, next_w))

        return dist[gh][gw] if dist[gh][gw] != INF else -1



    ans = bfs_2d(A, Ch, Cw, Dh, Dw, H, W)

    print(ans)





if __name__ == '__main__':

    main()

INF = 10**9 + 7


def main():
    from heapq import heappop, heappush

    H, W = (int(i) for i in input().split())
    Ch, Cw = (int(i) - 1 for i in input().split())
    Dh, Dw = (int(i) - 1 for i in input().split())
    A = [eval(input()) for i in range(H)]
    dist = [[INF] * W for _ in range(H)]
    dist[Ch][Cw] = 0
    que = [(0, Ch * W + Cw)]
    while que:
        u_dist, u = heappop(que)
        uh, uw = u // W, u % W
        if dist[uh][uw] < u_dist:
            continue
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                nh = uh + dh
                nw = uw + dw
                if nh < 0 or H <= nh or nw < 0 or W <= nw:
                    continue
                if A[nh][nw] == "#":
                    continue
                cost = 0 if abs(dh) + abs(dw) <= 1 else 1
                if dist[nh][nw] > dist[uh][uw] + cost:
                    dist[nh][nw] = dist[uh][uw] + cost
                    heappush(que, (dist[nh][nw], nh * W + nw))
    if dist[Dh][Dw] == INF:
        print((-1))
    else:
        print((dist[Dh][Dw]))
    # for h in range(H):
    #     print(dist[h])


if __name__ == "__main__":
    main()

# 01BFS
INF = 10**9 + 7


def main():
    from collections import deque

    H, W = (int(i) for i in input().split())
    Ch, Cw = (int(i) - 1 for i in input().split())
    Dh, Dw = (int(i) - 1 for i in input().split())
    A = [eval(input()) for i in range(H)]

    def bfs_2d(c, sy, sx, gh, gw, H, W):
        que = deque()
        dist = [[INF] * W for i in range(H)]
        dist[sy][sx] = 0
        que.append((sy, sx))
        while que:
            uh, uw = que.popleft()
            for dh in range(-2, 3):
                for dw in range(-2, 3):
                    next_h = uh + dh
                    next_w = uw + dw
                    cost = 0 if abs(dh) + abs(dw) <= 1 else 1
                    if not (0 <= next_h < H and 0 <= next_w < W):
                        continue
                    if c[next_h][next_w] == "#":
                        continue
                    if dist[next_h][next_w] > dist[uh][uw] + cost:
                        dist[next_h][next_w] = dist[uh][uw] + cost
                        if cost == 1:
                            que.append((next_h, next_w))
                        else:
                            que.appendleft((next_h, next_w))
        return dist[gh][gw] if dist[gh][gw] != INF else -1

    ans = bfs_2d(A, Ch, Cw, Dh, Dw, H, W)
    print(ans)


if __name__ == "__main__":
    main()

+# 01BFS -    from heapq import heappop, heappush +    from collections import deque -    dist = [[INF] * W for _ in range(H)] -    dist[Ch][Cw] = 0 -    que = [(0, Ch * W + Cw)] -    while que: -        u_dist, u = heappop(que) -        uh, uw = u // W, u % W -        if dist[uh][uw] < u_dist: -            continue -        for dh in range(-2, 3): -            for dw in range(-2, 3): -                nh = uh + dh -                nw = uw + dw -                if nh < 0 or H <= nh or nw < 0 or W <= nw: -                    continue -                if A[nh][nw] == "#": -                    continue -                cost = 0 if abs(dh) + abs(dw) <= 1 else 1 -                if dist[nh][nw] > dist[uh][uw] + cost: -                    dist[nh][nw] = dist[uh][uw] + cost -                    heappush(que, (dist[nh][nw], nh * W + nw)) -    if dist[Dh][Dw] == INF: -        print((-1)) -    else: -        print((dist[Dh][Dw])) -    # for h in range(H): -    #     print(dist[h]) + +    def bfs_2d(c, sy, sx, gh, gw, H, W): +        que = deque() +        dist = [[INF] * W for i in range(H)] +        dist[sy][sx] = 0 +        que.append((sy, sx)) +        while que: +            uh, uw = que.popleft() +            for dh in range(-2, 3): +                for dw in range(-2, 3): +                    next_h = uh + dh +                    next_w = uw + dw +                    cost = 0 if abs(dh) + abs(dw) <= 1 else 1 +                    if not (0 <= next_h < H and 0 <= next_w < W): +                        continue +                    if c[next_h][next_w] == "#": +                        continue +                    if dist[next_h][next_w] > dist[uh][uw] + cost: +                        dist[next_h][next_w] = dist[uh][uw] + cost +                        if cost == 1: +                            que.append((next_h, next_w)) +                        else: +                            que.appendleft((next_h, next_w)) +        return dist[gh][gw] if dist[gh][gw] != INF else -1 + +    ans = bfs_2d(A, Ch, Cw, Dh, Dw, H, W) +    print(ans)