from collections import deque
H,W = map(int, input().split())
Ch,Cw = map(int, input().split())
Dh,Dw = map(int, input().split())

edge = [["#" for _ in range(W+2)]]
center = [["#"] + list(input()) + ["#"] for i in range(H)]
A_array = edge + center + edge

#セルをグループ毎に分割
cells_grouped = dict()
group_no = 1

edge = [[1 for _ in range(W+2)]]
center = [[1] + [0 for _ in range(W+2)] + [1] for i in range(H)] #提出用
is_visited = edge + center + edge

edge = [[0 for _ in range(W+2)]]
center = [[0] + [0 for _ in range(W+2)] + [0] for i in range(H)] #提出用
cell_group_dict = edge + center + edge

for i in range(1,H+1):
    for j in range(1,W+1):
        #print(i,j,A_array[i][j])
        if is_visited[i][j] == 0:
            is_visited[i][j] = 1
            
            if A_array[i][j] == ".":
                #function 幅優先探索(v)
                search_target_deque = deque() #    Q ← 空のキュー
                search_target_deque.append((i,j)) #    v を Q に追加
                grouped_cells = set()
                grouped_cells.add((i,j))

                while len(search_target_deque) != 0: #    while Q が空ではない do
                    m,n = search_target_deque.popleft() #        v ← Q から取り出す
                    #        v を処理する
                    cell_group_dict[m][n] = group_no
                    neighbor_nodes = [(m+1,n),(m-1,n),(m,n+1),(m,n-1)]
                    for s,t in neighbor_nodes: #        for each v に接続している頂点 i do
                        if is_visited[s][t] == 0: # if i が未訪問 then
                            is_visited[s][t] = 1 #                iに訪問済みの印を付ける
                            if A_array[s][t] == ".":
                                search_target_deque.append((s,t)) #                i を Q に追加
                                grouped_cells.add((s,t))

                cells_grouped[group_no] = grouped_cells
                group_no += 1

                
#print("cells_grouped")
#print(cells_grouped)

#print("cell_group_dict")
#print(cell_group_dict)
                
#各グループから行けるグループを得る
warpable_group = {i:set() for i in range(group_no)}
for i in range(1,group_no):
    for cell in cells_grouped[i]:
        y = cell[0]
        x = cell[1]
        for m in range(y-2,y+3,1):
            if 1<=m<=H:
                for n in range(x-2,x+3,1):
                    if 1<=n<=W and cell_group_dict[m][n]!=0 and cell_group_dict[m][n]!=group_no:
                        warpable_group[i].add(cell_group_dict[m][n])

#print("warpable_group")
#print(warpable_group)

#function 幅優先探索(v)
distance_from_start = ["?" for i in range(group_no)] #startからの距離
is_visited = [0 for i in range(group_no)] #訪問フラグ

v = cell_group_dict[Ch][Cw]

search_target_deque = deque() #    Q ← 空のキュー
is_visited[v] = 1 #    v に訪問済みの印を付ける
search_target_deque.append(v) #    v を Q に追加
distance_from_start[v] = 0

while len(search_target_deque) != 0: #    while Q が空ではない do
    node = search_target_deque.popleft() #        v ← Q から取り出す
    #        v を処理する
    if node == cell_group_dict[Dh][Dw]:
        print(distance_from_start[node])
        break
    
    near_nodes = warpable_group[node]
    
    for near_node in near_nodes: #        for each v に接続している頂点 i do

        if is_visited[near_node] == 0: #            if i が未訪問 then
            is_visited[near_node] = 1 #                i に訪問済みの印を付ける
            search_target_deque.append(near_node) #                i を Q に追加
            distance_from_start[near_node] = distance_from_start[node] + 1
else:
    print(-1)

Measured memory_bytes: 249028