# 01-BFS

# 参考: White_Pie_46

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

# 01-BFSのちょっと丁寧な解説 - ARMERIA

# https://betrue12.hateblo.jp/entry/2018/12/08/000020



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)]



    can_walk = ((1, 0), (0, 1), (-1, 0), (0, -1))

    gen_warp_can_reach = (

        (h, w, (h, w) in can_walk)  # 移動先，徒歩で行けるか

        for h in range(-2, 3) for w in range(-2, 3) if not h == 0 == w)

    warp_can_reach = set(gen_warp_can_reach)

    qu = deque()

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



    while qu:

        h, w, warps = qu.popleft()

        if h == dest_h and w == dest_w:

            return warps

        if grid[h][w] == 'R':

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

            continue

        grid[h][w] = 'R'  # reached



        for h_delta, w_delta, walking 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 walking:

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

                    else:

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

    return -1





if __name__ == '__main__':

    print((main()))

# 01-BFS

# 参考: White_Pie_46

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

# 01-BFSのちょっと丁寧な解説 - ARMERIA

# https://betrue12.hateblo.jp/entry/2018/12/08/000020



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)]



    can_walk = ((1, 0), (0, 1), (-1, 0), (0, -1))

    gen_warp_can_reach = (

        (h, w, (h, w) in can_walk)  # 移動先，徒歩で行けるか

        for h in range(-2, 3) for w in range(-2, 3) if not h == 0 == w)

    warp_can_reach = tuple(gen_warp_can_reach)

    qu = deque()

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



    while qu:

        h, w, warps = qu.popleft()

        if h == dest_h and w == dest_w:

            return warps

        if grid[h][w] == 'R':

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

            continue

        grid[h][w] = 'R'  # reached



        for h_delta, w_delta, walking 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 walking:

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

                    else:

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

    return -1





if __name__ == '__main__':

    print((main()))

# 01-BFS
# 参考: White_Pie_46
# https://atcoder.jp/contests/abc176/submissions/16168825
# 01-BFSのちょっと丁寧な解説 - ARMERIA
# https://betrue12.hateblo.jp/entry/2018/12/08/000020
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)]
    can_walk = ((1, 0), (0, 1), (-1, 0), (0, -1))
    gen_warp_can_reach = (
        (h, w, (h, w) in can_walk)  # 移動先，徒歩で行けるか
        for h in range(-2, 3)
        for w in range(-2, 3)
        if not h == 0 == w
    )
    warp_can_reach = set(gen_warp_can_reach)
    qu = deque()
    qu.append((init_h, init_w, 0))  # 座標とワープ回数を保持
    while qu:
        h, w, warps = qu.popleft()
        if h == dest_h and w == dest_w:
            return warps
        if grid[h][w] == "R":
            # 同じマスを複数回キューに追加しているかもしれない。
            continue
        grid[h][w] = "R"  # reached
        for h_delta, w_delta, walking 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 walking:
                        qu.appendleft((new_h, new_w, warps))
                    else:
                        qu.append((new_h, new_w, warps + 1))
    return -1


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

# 01-BFS
# 参考: White_Pie_46
# https://atcoder.jp/contests/abc176/submissions/16168825
# 01-BFSのちょっと丁寧な解説 - ARMERIA
# https://betrue12.hateblo.jp/entry/2018/12/08/000020
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)]
    can_walk = ((1, 0), (0, 1), (-1, 0), (0, -1))
    gen_warp_can_reach = (
        (h, w, (h, w) in can_walk)  # 移動先，徒歩で行けるか
        for h in range(-2, 3)
        for w in range(-2, 3)
        if not h == 0 == w
    )
    warp_can_reach = tuple(gen_warp_can_reach)
    qu = deque()
    qu.append((init_h, init_w, 0))  # 座標とワープ回数を保持
    while qu:
        h, w, warps = qu.popleft()
        if h == dest_h and w == dest_w:
            return warps
        if grid[h][w] == "R":
            # 同じマスを複数回キューに追加しているかもしれない。
            continue
        grid[h][w] = "R"  # reached
        for h_delta, w_delta, walking 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 walking:
                        qu.appendleft((new_h, new_w, warps))
                    else:
                        qu.append((new_h, new_w, warps + 1))
    return -1


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

-    warp_can_reach = set(gen_warp_can_reach) +    warp_can_reach = tuple(gen_warp_can_reach)