import sys

from collections import deque, defaultdict



sys.setrecursionlimit(10000)



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

C = tuple(map(int, input().split()))

D = tuple(map(int, input().split()))

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



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



def dfs(h, w, i):

    S[h][w] = i



    for dh, dw in d:

        nh = h + dh

        nw = w + dw

        if nh < 0 or nh >= H or nw < 0 or nw >= W:

            continue

        if S[nh][nw] != '.':

            continue

        dfs(nh, nw, i)



# 番号を振る

count = 1

for h in range(H):

    for w in range(W):

        if S[h][w] != '.':

            continue

        dfs(h, w, count)

        count += 1



# グラフを構築

graph = {}

for i in range(1, count):

    graph[i] = set()



for h in range(H):

    for w in range(W):

        if S[h][w] == '#':

            continue



        for dh in range(-2, 3):

            for dw in range(-2, 3):

                nh = h + dh

                nw = w + dw

                if nh < 0 or nh >= H or nw < 0 or nw >= W:

                    continue

                if S[nh][nw] == '#':

                    continue

                graph[S[h][w]].add(S[nh][nw])



start = S[C[0]-1][C[1]-1]

goal = S[D[0]-1][D[1]-1]



# 最短経路を求める

def bfs(start, goal):

    queue = deque([start])

    visited = set()

    visited.add(start)

    dist = defaultdict(int)

    dist[start] = 0

    while queue:

        current_vertex = queue.popleft()

        for v in graph[current_vertex]:

            if v not in visited:

                visited.add(v)

                queue.append(v)

                dist[v] = dist[current_vertex] + 1

    return dist[goal] if goal in dist else -1



ans = bfs(start, goal)



print(ans)

import sys

from collections import deque, defaultdict



sys.setrecursionlimit(10000)



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

C = tuple(map(int, input().split()))

D = tuple(map(int, input().split()))

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



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





def dfs(h, w, i):

    S[h][w] = i



    for dh, dw in d:

        nh = h + dh

        nw = w + dw

        if nh < 0 or nh >= H or nw < 0 or nw >= W:

            continue

        if S[nh][nw] != '.':

            continue

        dfs(nh, nw, i)





# 領域毎に番号を振る

count = 1

for h in range(H):

    for w in range(W):

        if S[h][w] != '.':

            continue

        dfs(h, w, count)

        count += 1



# 各領域をノードとしてグラフを構築

graph = defaultdict(set)

for h in range(H):

    for w in range(W):

        if S[h][w] == '#':

            continue



        for dh in range(-2, 3):

            for dw in range(-2, 3):

                nh = h + dh

                nw = w + dw

                if nh < 0 or nh >= H or nw < 0 or nw >= W:

                    continue

                if S[nh][nw] == '#':

                    continue

                graph[S[h][w]].add(S[nh][nw])



start = S[C[0]-1][C[1]-1]

goal = S[D[0]-1][D[1]-1]





def shortest_dist(graph, start, goal):

    queue = deque([start])

    visited = set()

    visited.add(start)

    dist = defaultdict(int)

    dist[start] = 0

    while queue:

        current_vertex = queue.popleft()

        if current_vertex == goal:

            return dist[goal]

        for v in graph[current_vertex]:

            if v not in visited:

                visited.add(v)

                queue.append(v)

                dist[v] = dist[current_vertex] + 1

    return -1



# 最短距離を求める

ans = shortest_dist(graph, start, goal)



print(ans)

import sys
from collections import deque, defaultdict

sys.setrecursionlimit(10000)
H, W = list(map(int, input().split()))
C = tuple(map(int, input().split()))
D = tuple(map(int, input().split()))
S = [list(eval(input())) for _ in range(H)]
d = [(0, 1), (1, 0), (0, -1), (-1, 0)]


def dfs(h, w, i):
    S[h][w] = i
    for dh, dw in d:
        nh = h + dh
        nw = w + dw
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if S[nh][nw] != ".":
            continue
        dfs(nh, nw, i)


# 番号を振る
count = 1
for h in range(H):
    for w in range(W):
        if S[h][w] != ".":
            continue
        dfs(h, w, count)
        count += 1
# グラフを構築
graph = {}
for i in range(1, count):
    graph[i] = set()
for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                nh = h + dh
                nw = w + dw
                if nh < 0 or nh >= H or nw < 0 or nw >= W:
                    continue
                if S[nh][nw] == "#":
                    continue
                graph[S[h][w]].add(S[nh][nw])
start = S[C[0] - 1][C[1] - 1]
goal = S[D[0] - 1][D[1] - 1]
# 最短経路を求める
def bfs(start, goal):
    queue = deque([start])
    visited = set()
    visited.add(start)
    dist = defaultdict(int)
    dist[start] = 0
    while queue:
        current_vertex = queue.popleft()
        for v in graph[current_vertex]:
            if v not in visited:
                visited.add(v)
                queue.append(v)
                dist[v] = dist[current_vertex] + 1
    return dist[goal] if goal in dist else -1


ans = bfs(start, goal)
print(ans)

import sys
from collections import deque, defaultdict

sys.setrecursionlimit(10000)
H, W = list(map(int, input().split()))
C = tuple(map(int, input().split()))
D = tuple(map(int, input().split()))
S = [list(eval(input())) for _ in range(H)]
d = [(0, 1), (1, 0), (0, -1), (-1, 0)]


def dfs(h, w, i):
    S[h][w] = i
    for dh, dw in d:
        nh = h + dh
        nw = w + dw
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if S[nh][nw] != ".":
            continue
        dfs(nh, nw, i)


# 領域毎に番号を振る
count = 1
for h in range(H):
    for w in range(W):
        if S[h][w] != ".":
            continue
        dfs(h, w, count)
        count += 1
# 各領域をノードとしてグラフを構築
graph = defaultdict(set)
for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                nh = h + dh
                nw = w + dw
                if nh < 0 or nh >= H or nw < 0 or nw >= W:
                    continue
                if S[nh][nw] == "#":
                    continue
                graph[S[h][w]].add(S[nh][nw])
start = S[C[0] - 1][C[1] - 1]
goal = S[D[0] - 1][D[1] - 1]


def shortest_dist(graph, start, goal):
    queue = deque([start])
    visited = set()
    visited.add(start)
    dist = defaultdict(int)
    dist[start] = 0
    while queue:
        current_vertex = queue.popleft()
        if current_vertex == goal:
            return dist[goal]
        for v in graph[current_vertex]:
            if v not in visited:
                visited.add(v)
                queue.append(v)
                dist[v] = dist[current_vertex] + 1
    return -1


# 最短距離を求める
ans = shortest_dist(graph, start, goal)
print(ans)

-# 番号を振る +# 領域毎に番号を振る -# グラフを構築 -graph = {} -for i in range(1, count): -    graph[i] = set() +# 各領域をノードとしてグラフを構築 +graph = defaultdict(set) -# 最短経路を求める -def bfs(start, goal): + + +def shortest_dist(graph, start, goal): +        if current_vertex == goal: +            return dist[goal] -    return dist[goal] if goal in dist else -1 +    return -1 -ans = bfs(start, goal) +# 最短距離を求める +ans = shortest_dist(graph, start, goal)