# 参考: White_Pie_46

# https://atcoder.jp/contests/abc176/submissions/16168825

from collections import deque





def main():

    height, width = [int(x) for x in input().split()]

    init_h, init_w = [int(x) - 1 for x in input().split()]

    dest_h, dest_w = [int(x) - 1 for x in input().split()]

    grid = [list(eval(input())) for _ in range(height)]



    adjacents = (

        (1, 0),

        (0, 1),

        (-1, 0),

        (0, -1)

    )



    def gen_warp_can_reach():

        for h in range(-2, 3):

            for w in range(-2, 3):

                if h == 0 == w:

                    continue

                if (h, w) in adjacents:

                    continue

                yield (h, w)

    warp_can_reach = tuple(gen_warp_can_reach())

    qu = deque()

    qu.append((init_h, init_w, 0))  # 座標とワープ回数を保持

    reached = set()



    while qu:

        h, w, warps = qu.popleft()

        if h == dest_h and w == dest_w:

            return warps

        if (h, w) in reached:

            # 同じマスを複数回キューに追加しているかもしれない。

            # 短距離で調査済みなら再調査は不要。

            continue

        reached.add((h, w))



        for h_delta, w_delta in adjacents:

            new_h, new_w = h + h_delta, w + w_delta

            if 0 <= new_h < height and 0 <= new_w < width:

                if grid[new_h][new_w] == '.':

                    qu.appendleft((new_h, new_w, warps))



        for h_delta, w_delta in warp_can_reach:

            new_h, new_w = h + h_delta, w + w_delta

            if 0 <= new_h < height and 0 <= new_w < width:

                if grid[new_h][new_w] == '.':

                    qu.append((new_h, new_w, warps + 1))

    return -1





if __name__ == '__main__':

    print((main()))

# 参考: White_Pie_46

# https://atcoder.jp/contests/abc176/submissions/16168825

from collections import deque





def main():

    height, width = [int(x) for x in input().split()]

    init_h, init_w = [int(x) - 1 for x in input().split()]

    dest_h, dest_w = [int(x) - 1 for x in input().split()]

    grid = [list(eval(input())) for _ in range(height)]



    adjacents = (

        (1, 0),

        (0, 1),

        (-1, 0),

        (0, -1)

    )



    def gen_warp_can_reach():

        for h in range(-2, 3):

            for w in range(-2, 3):

                if h == 0 == w:

                    continue

                if (h, w) in adjacents:

                    continue

                yield (h, w)

    warp_can_reach = tuple(gen_warp_can_reach())

    qu = deque()

    qu.append((init_h, init_w, 0))  # 座標とワープ回数を保持

    reached = set()



    while qu:

        h, w, warps = qu.popleft()

        if h == dest_h and w == dest_w:

            return warps

        if (h, w) in reached:

            # 同じマスを複数回キューに追加しているかもしれない。

            # 短距離で調査済みなら再調査は不要。

            continue

        reached.add((h, w))



        for h_delta, w_delta in adjacents:

            new_h, new_w = h + h_delta, w + w_delta

            if 0 <= new_h < height and 0 <= new_w < width:

                if grid[new_h][new_w] == '.':

                    if (new_h, new_w) not in reached:

                        qu.appendleft((new_h, new_w, warps))



        for h_delta, w_delta in warp_can_reach:

            new_h, new_w = h + h_delta, w + w_delta

            if 0 <= new_h < height and 0 <= new_w < width:

                if grid[new_h][new_w] == '.':

                    if (new_h, new_w) not in reached:

                        qu.append((new_h, new_w, warps + 1))

    return -1





if __name__ == '__main__':

    print((main()))

# 参考: White_Pie_46
# https://atcoder.jp/contests/abc176/submissions/16168825
from collections import deque


def main():
    height, width = [int(x) for x in input().split()]
    init_h, init_w = [int(x) - 1 for x in input().split()]
    dest_h, dest_w = [int(x) - 1 for x in input().split()]
    grid = [list(eval(input())) for _ in range(height)]
    adjacents = ((1, 0), (0, 1), (-1, 0), (0, -1))

    def gen_warp_can_reach():
        for h in range(-2, 3):
            for w in range(-2, 3):
                if h == 0 == w:
                    continue
                if (h, w) in adjacents:
                    continue
                yield (h, w)

    warp_can_reach = tuple(gen_warp_can_reach())
    qu = deque()
    qu.append((init_h, init_w, 0))  # 座標とワープ回数を保持
    reached = set()
    while qu:
        h, w, warps = qu.popleft()
        if h == dest_h and w == dest_w:
            return warps
        if (h, w) in reached:
            # 同じマスを複数回キューに追加しているかもしれない。
            # 短距離で調査済みなら再調査は不要。
            continue
        reached.add((h, w))
        for h_delta, w_delta in adjacents:
            new_h, new_w = h + h_delta, w + w_delta
            if 0 <= new_h < height and 0 <= new_w < width:
                if grid[new_h][new_w] == ".":
                    qu.appendleft((new_h, new_w, warps))
        for h_delta, w_delta in warp_can_reach:
            new_h, new_w = h + h_delta, w + w_delta
            if 0 <= new_h < height and 0 <= new_w < width:
                if grid[new_h][new_w] == ".":
                    qu.append((new_h, new_w, warps + 1))
    return -1


if __name__ == "__main__":
    print((main()))

# 参考: White_Pie_46
# https://atcoder.jp/contests/abc176/submissions/16168825
from collections import deque


def main():
    height, width = [int(x) for x in input().split()]
    init_h, init_w = [int(x) - 1 for x in input().split()]
    dest_h, dest_w = [int(x) - 1 for x in input().split()]
    grid = [list(eval(input())) for _ in range(height)]
    adjacents = ((1, 0), (0, 1), (-1, 0), (0, -1))

    def gen_warp_can_reach():
        for h in range(-2, 3):
            for w in range(-2, 3):
                if h == 0 == w:
                    continue
                if (h, w) in adjacents:
                    continue
                yield (h, w)

    warp_can_reach = tuple(gen_warp_can_reach())
    qu = deque()
    qu.append((init_h, init_w, 0))  # 座標とワープ回数を保持
    reached = set()
    while qu:
        h, w, warps = qu.popleft()
        if h == dest_h and w == dest_w:
            return warps
        if (h, w) in reached:
            # 同じマスを複数回キューに追加しているかもしれない。
            # 短距離で調査済みなら再調査は不要。
            continue
        reached.add((h, w))
        for h_delta, w_delta in adjacents:
            new_h, new_w = h + h_delta, w + w_delta
            if 0 <= new_h < height and 0 <= new_w < width:
                if grid[new_h][new_w] == ".":
                    if (new_h, new_w) not in reached:
                        qu.appendleft((new_h, new_w, warps))
        for h_delta, w_delta in warp_can_reach:
            new_h, new_w = h + h_delta, w + w_delta
            if 0 <= new_h < height and 0 <= new_w < width:
                if grid[new_h][new_w] == ".":
                    if (new_h, new_w) not in reached:
                        qu.append((new_h, new_w, warps + 1))
    return -1


if __name__ == "__main__":
    print((main()))

-                    qu.appendleft((new_h, new_w, warps)) +                    if (new_h, new_w) not in reached: +                        qu.appendleft((new_h, new_w, warps)) -                    qu.append((new_h, new_w, warps + 1)) +                    if (new_h, new_w) not in reached: +                        qu.append((new_h, new_w, warps + 1))