import sys

input = sys.stdin.readline





def main():

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

    ch, cw = list(map(int, input().split()))

    dh, dw = list(map(int, input().split()))

    

    ch += 1

    cw += 1

    dh += 1

    dw += 1

    

    

    s = ["#"*(w+4)]

    s.append("#"*(w+4))

    for i in range(h):

        s.append("##" + input()[:-1] + "##")

    s.append("#"*(w+4))

    s.append("#"*(w+4))

    

    

    ans = [[-1]*(w+4) for _ in range(h+4)]

    for i in range(h+4):

        for j in range(w+4):

            if s[i][j] == "#":

                ans[i][j] = -2

    ans[ch][cw] = 0

    

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

    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \

             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \

             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \

             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \

             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

        

    not_yet = [(ch, cw)]

    one_time = []

    

    while len(not_yet) > 0:

        x, y = not_yet.pop()

        one_time.append((x, y))

        for (p, q) in move:

            v1, v2 = x+p, y+q

            if ans[v1][v2] == -1:

                not_yet.append((v1, v2))

                ans[v1][v2] = ans[x][y]

                

        if len(not_yet) == 0:

            while len(one_time) > 0:

                x2, y2 = one_time.pop()

                for (v1, v2) in move2:

                    i, j = x2+v1, y2+v2

                    if ans[i][j] == -1:

                        ans[i][j] = ans[x2][y2]+1

                        not_yet.append((i, j))

          

    print((ans[dh][dw]))

    

    

    

if __name__ == "__main__":

    main()

#----------[インポート]----------#

from collections import deque



def main():

    #----------[入力受取・初期設定]----------#

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

    C = tuple(map(int, input().split())) # 魔法の位置

    D = tuple(map(int, input().split())) # ゴール

    D_y = D[0] -1 # 0スタート

    D_x = D[1] -1 # 0スタート

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

    visited = [[-1] * W for _ in range(H)]

    visited[C[0]-1][C[1]-1] = 0 # 初期値

    moves = [(1, 0), (0, 1), (-1, 0), (0, -1)] # 普通の移動

    main_q = deque()

    magic_q = deque() # マジック用のキュー

    main_q.append((C[0]-1, C[1]-1))

    # magic_q.append((C[0]-1, C[1]-1))



    #----------[BFS]----------#

    while main_q:

        y, x = main_q.pop()

        magic_q.append((y, x))

        for move in moves:

            dy, dx = move

            moved_y = y + dy

            moved_x = x + dx



            if moved_y < 0 or H -1 < moved_y or moved_x < 0 or W -1 < moved_x:

                continue

            if S[moved_y][moved_x] == '#':

                continue

            if S[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:

                main_q.append((moved_y, moved_x))

                visited[moved_y][moved_x] = visited[y][x]



        #----------[マジック用のBFS]----------#

        if not main_q: # main_qが空になった場合は探索済みからマジックを使う

            while magic_q:

                y, x = magic_q.pop()

                for dy in range(-2, 3):

                    for dx in range(-2, 3):

                        moved_y = y + dy

                        moved_x = x + dx



                        if moved_y < 0 or H -1 < moved_y or moved_x < 0 or W -1 < moved_x:

                            continue

                        if S[moved_y][moved_x] == '#':

                            continue

                        if S[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:

                            main_q.append((moved_y, moved_x))

                            visited[moved_y][moved_x] = visited[y][x] + 1



    answer = visited[D_y][D_x]

    print(answer)



if __name__ == "__main__":

    main()
import sys

input = sys.stdin.readline


def main():
    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    s = ["#" * (w + 4)]
    s.append("#" * (w + 4))
    for i in range(h):
        s.append("##" + input()[:-1] + "##")
    s.append("#" * (w + 4))
    s.append("#" * (w + 4))
    ans = [[-1] * (w + 4) for _ in range(h + 4)]
    for i in range(h + 4):
        for j in range(w + 4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[ch][cw] = 0
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [
        (-2, -2),
        (-2, -1),
        (-2, 0),
        (-2, 1),
        (-2, 2),
        (-1, -2),
        (-1, -1),
        (-1, 0),
        (-1, 1),
        (-1, 2),
        (0, -2),
        (0, -1),
        (0, 0),
        (0, 1),
        (0, 2),
        (1, -2),
        (1, -1),
        (1, 0),
        (1, 1),
        (1, 2),
        (2, -2),
        (2, -1),
        (2, 0),
        (2, 1),
        (2, 2),
    ]
    not_yet = [(ch, cw)]
    one_time = []
    while len(not_yet) > 0:
        x, y = not_yet.pop()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x + p, y + q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.pop()
                for (v1, v2) in move2:
                    i, j = x2 + v1, y2 + v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2] + 1
                        not_yet.append((i, j))
    print((ans[dh][dw]))


if __name__ == "__main__":
    main()

# ----------[インポート]----------#
from collections import deque


def main():
    # ----------[入力受取・初期設定]----------#
    H, W = list(map(int, input().split()))
    C = tuple(map(int, input().split()))  # 魔法の位置
    D = tuple(map(int, input().split()))  # ゴール
    D_y = D[0] - 1  # 0スタート
    D_x = D[1] - 1  # 0スタート
    S = [list(eval(input())) for _ in range(H)]
    visited = [[-1] * W for _ in range(H)]
    visited[C[0] - 1][C[1] - 1] = 0  # 初期値
    moves = [(1, 0), (0, 1), (-1, 0), (0, -1)]  # 普通の移動
    main_q = deque()
    magic_q = deque()  # マジック用のキュー
    main_q.append((C[0] - 1, C[1] - 1))
    # magic_q.append((C[0]-1, C[1]-1))
    # ----------[BFS]----------#
    while main_q:
        y, x = main_q.pop()
        magic_q.append((y, x))
        for move in moves:
            dy, dx = move
            moved_y = y + dy
            moved_x = x + dx
            if moved_y < 0 or H - 1 < moved_y or moved_x < 0 or W - 1 < moved_x:
                continue
            if S[moved_y][moved_x] == "#":
                continue
            if S[moved_y][moved_x] == "." and visited[moved_y][moved_x] == -1:
                main_q.append((moved_y, moved_x))
                visited[moved_y][moved_x] = visited[y][x]
        # ----------[マジック用のBFS]----------#
        if not main_q:  # main_qが空になった場合は探索済みからマジックを使う
            while magic_q:
                y, x = magic_q.pop()
                for dy in range(-2, 3):
                    for dx in range(-2, 3):
                        moved_y = y + dy
                        moved_x = x + dx
                        if (
                            moved_y < 0
                            or H - 1 < moved_y
                            or moved_x < 0
                            or W - 1 < moved_x
                        ):
                            continue
                        if S[moved_y][moved_x] == "#":
                            continue
                        if (
                            S[moved_y][moved_x] == "."
                            and visited[moved_y][moved_x] == -1
                        ):
                            main_q.append((moved_y, moved_x))
                            visited[moved_y][moved_x] = visited[y][x] + 1
    answer = visited[D_y][D_x]
    print(answer)


if __name__ == "__main__":
    main()

-import sys - -input = sys.stdin.readline +from collections import deque -    h, w = list(map(int, input().split())) -    ch, cw = list(map(int, input().split())) -    dh, dw = list(map(int, input().split())) -    ch += 1 -    cw += 1 -    dh += 1 -    dw += 1 -    s = ["#" * (w + 4)] -    s.append("#" * (w + 4)) -    for i in range(h): -        s.append("##" + input()[:-1] + "##") -    s.append("#" * (w + 4)) -    s.append("#" * (w + 4)) -    ans = [[-1] * (w + 4) for _ in range(h + 4)] -    for i in range(h + 4): -        for j in range(w + 4): -            if s[i][j] == "#": -                ans[i][j] = -2 -    ans[ch][cw] = 0 -    move = [(-1, 0), (1, 0), (0, -1), (0, 1)] -    move2 = [ -        (-2, -2), -        (-2, -1), -        (-2, 0), -        (-2, 1), -        (-2, 2), -        (-1, -2), -        (-1, -1), -        (-1, 0), -        (-1, 1), -        (-1, 2), -        (0, -2), -        (0, -1), -        (0, 0), -        (0, 1), -        (0, 2), -        (1, -2), -        (1, -1), -        (1, 0), -        (1, 1), -        (1, 2), -        (2, -2), -        (2, -1), -        (2, 0), -        (2, 1), -        (2, 2), -    ] -    not_yet = [(ch, cw)] -    one_time = [] -    while len(not_yet) > 0: -        x, y = not_yet.pop() -        one_time.append((x, y)) -        for (p, q) in move: -            v1, v2 = x + p, y + q -            if ans[v1][v2] == -1: -                not_yet.append((v1, v2)) -                ans[v1][v2] = ans[x][y] -        if len(not_yet) == 0: -            while len(one_time) > 0: -                x2, y2 = one_time.pop() -                for (v1, v2) in move2: -                    i, j = x2 + v1, y2 + v2 -                    if ans[i][j] == -1: -                        ans[i][j] = ans[x2][y2] + 1 -                        not_yet.append((i, j)) -    print((ans[dh][dw])) +    H, W = list(map(int, input().split())) +    C = tuple(map(int, input().split()))  # 魔法の位置 +    D = tuple(map(int, input().split()))  # ゴール +    D_y = D[0] - 1  # 0スタート +    D_x = D[1] - 1  # 0スタート +    S = [list(eval(input())) for _ in range(H)] +    visited = [[-1] * W for _ in range(H)] +    visited[C[0] - 1][C[1] - 1] = 0  # 初期値 +    moves = [(1, 0), (0, 1), (-1, 0), (0, -1)]  # 普通の移動 +    main_q = deque() +    magic_q = deque()  # マジック用のキュー +    main_q.append((C[0] - 1, C[1] - 1)) +    # magic_q.append((C[0]-1, C[1]-1)) +    while main_q: +        y, x = main_q.pop() +        magic_q.append((y, x)) +        for move in moves: +            dy, dx = move +            moved_y = y + dy +            moved_x = x + dx +            if moved_y < 0 or H - 1 < moved_y or moved_x < 0 or W - 1 < moved_x: +                continue +            if S[moved_y][moved_x] == "#": +                continue +            if S[moved_y][moved_x] == "." and visited[moved_y][moved_x] == -1: +                main_q.append((moved_y, moved_x)) +                visited[moved_y][moved_x] = visited[y][x] +        if not main_q:  # main_qが空になった場合は探索済みからマジックを使う +            while magic_q: +                y, x = magic_q.pop() +                for dy in range(-2, 3): +                    for dx in range(-2, 3): +                        moved_y = y + dy +                        moved_x = x + dx +                        if ( +                            moved_y < 0 +                            or H - 1 < moved_y +                            or moved_x < 0 +                            or W - 1 < moved_x +                        ): +                            continue +                        if S[moved_y][moved_x] == "#": +                            continue +                        if ( +                            S[moved_y][moved_x] == "." +                            and visited[moved_y][moved_x] == -1 +                        ): +                            main_q.append((moved_y, moved_x)) +                            visited[moved_y][moved_x] = visited[y][x] + 1 +    answer = visited[D_y][D_x] +    print(answer)