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

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()
#----------[インポート]----------#

from collections import deque

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

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

#----------[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)
# ----------[インポート]----------#
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()

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

# ----------[入力受取・初期設定]----------#
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))
# ----------[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)

- -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)) -    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) - - -if __name__ == "__main__": -    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)) +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)