from collections import deque
import math
import copy

def main():
    hh,ww = list(map(int,input().split()))
    ch, cw = list(map(int,input().split()))
    dh, dw = list(map(int,input().split()))
    maze=[list(input()) for l in range(hh)]
    segid = 0

    # Maze preprocess
    for y in range(hh):
        for x in range(ww):
            if maze[y][x]=='.':
                maze[y][x]=math.inf
            elif maze[y][x] == '#':
                maze[y][x]=int(-1)
# segmentation
    for m in range(hh):
        for n in range(ww):
            if maze[m][n] == math.inf:
                maze[m][n] = segid
                que = deque([[m,n]])
                while que:
#                    print(que, maze)
                    current_hh, current_ww = que.popleft()
                    for i, j in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                        next_hh, next_ww = current_hh + i, current_ww + j
                        if (next_hh >= 0) and (next_ww >= 0) and (next_hh < hh) and (next_ww < ww):
                            dist = maze[next_hh][next_ww]
                            if dist == math.inf:
                                maze[next_hh][next_ww] = segid
                                que.append([next_hh, next_ww])
                segid += 1
#                print(segid)

#create graph
    flag_segid = [1]*segid
    graph = [[] for _ in range(segid)]
    for m in range(hh):
        for n in range(ww):
            current_segid = maze[m][n]
            if (current_segid >= 0 ) and (flag_segid[current_segid] == 1):
                que = deque([[m, n]])
                while que:
#                    print(que, maze)
                    current_hh, current_ww = que.popleft()
                    for i in [-2, -1, 0, 1, 2]:
                        for j in [-2, -1, 0, 1, 2]:
                            # print([i,j])
                            if (i == 0 and j == 0):
                                continue
                            next_hh, next_ww = current_hh + i, current_ww + j
                            if (next_hh >= 0) and (next_ww >= 0) and (next_hh < hh) and (next_ww < ww):
                                next_segid = maze[next_hh][next_ww]
                                if (next_segid != -1) and (next_segid != current_segid):
                                    if not (next_segid in graph[current_segid]):
                                        graph[current_segid].append(next_segid)
#                                        print(graph)
                #flag_segid[current_segid] = 0

#solve graph https://qiita.com/ta-ka/items/a023a11efe17ab097433
#https://qiita.com/shizuma/items/e08a76ab26073b21c207
#https://zalgo-official.com/blog/research-blog/competition-programming/bfs-and-dijkstra



    start_graph = maze[ch-1][cw-1]
    end_graph = maze[dh-1][dw-1]


#   print(graph)
#   print(start_graph, end_graph)

    qq = deque()
    cost = [math.inf] * segid
    cost[start_graph] = 0
    qq.append(start_graph)
    while qq:
        v = qq.popleft()
        for i in graph[v]:
            newcost = cost[v] + 1
            if cost[i] > newcost:
                cost[i] = newcost
                qq.append(i)
#    print(cost)
    if cost[end_graph] == math.inf:
        print(-1)
    else:
        print(cost[end_graph])

main()


Measured memory_bytes: 148468