### BUGGY CODE ###

from collections import deque

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
S = []
for _ in range(H):
    s = input()
    S.append(s)

# 上下左右に移動できるところは移動する
# 移動してもたどり着けない場合はワープをそれぞれのますで実行し、まだ到達していない箇所にいく
# これを繰り返す

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

wx = [-2, -1, 0, 1, 2]
wy = [-2, -1, 0, 1, 2]
dp = [[-1] * W for _ in range(H)]
dp[Ch - 1][Cw - 1] = 0


def bfs(x, y, cost, dp, d):
    while len(d) > 0:
        p = d.popleft()
        for i in range(4):
            if p[0] + dx[i] >= 0 and p[0] + dx[i] < W and p[1] + \
                    dy[i] >= 0 and p[1] + dy[i] < H:
                if S[p[1] + dy[i]][p[0] + dx[i]
                                   ] == '.' and dp[p[1] + dy[i]][p[0] + dx[i]] == -1:
                    d.append([p[0] + dx[i], p[1] + dy[i]])
                    dp[p[1] + dy[i]][p[0] + dx[i]] = cost

    return cost, dp


dw = deque()
ds = deque()
ds.append([Cw - 1, Ch - 1])
c = 0
while True:
    cost, dp = bfs(Cw - 1, Ch - 1, c, dp, ds)
    for i in range(H):
        for j in range(W):
            if dp[i][j] == cost:
                dw.append([j, i])
    check = False
    while len(dw) > 0:
        p = dw.popleft()
        for m in range(5):
            for n in range(5):
                if p[0] + wx[m] >= 0 and p[0] + wx[m] < W and p[1] + \
                        wy[n] >= 0 and p[1] + wy[n] < H:
                    if S[p[1] + wy[n]][p[0] + wx[m]
                                       ] == '.' and dp[p[1] + wy[n]][p[0] + wx[m]] == -1:
                        ds.append([p[0] + wx[m], p[1] + wy[n]])
                        dp[p[1] + wy[n]][p[0] + wx[m]] = cost + 1
                        check = True
    if check:
        c += 1
    else:
        break


print(dp[Dh - 1][Dw - 1])


### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
S = []
for _ in range(H):
    s = input()
    S.append(s)

# 上下左右に移動できるところは移動する
# 移動してもたどり着けない場合はワープをそれぞれのますで実行し、まだ到達していない箇所にいく
# これを繰り返す

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

wx = [-2, -1, 0, 1, 2]
wy = [-2, -1, 0, 1, 2]
dp = [[-1] * W for _ in range(H)]
dp[Ch - 1][Cw - 1] = 0


def bfs(x, y, cost, dp, d):
    while len(d) > 0:
        p = d.popleft()
        dw.append(p)
        for i in range(4):
            if p[0] + dx[i] >= 0 and p[0] + dx[i] < W and p[1] + \
                    dy[i] >= 0 and p[1] + dy[i] < H:
                if S[p[1] + dy[i]][p[0] + dx[i]
                                   ] == '.' and dp[p[1] + dy[i]][p[0] + dx[i]] == -1:
                    d.append([p[0] + dx[i], p[1] + dy[i]])
                    dw.append([p[0] + dx[i], p[1] + dy[i]])
                    dp[p[1] + dy[i]][p[0] + dx[i]] = cost

    return cost, dp


ds = deque()
ds.append([Cw - 1, Ch - 1])
c = 0
while True:
    dw = deque()
    cost, dp = bfs(Cw - 1, Ch - 1, c, dp, ds)
    check = False
    while len(dw) > 0:
        p = dw.popleft()
        for m in range(5):
            for n in range(5):
                if p[0] + wx[m] >= 0 and p[0] + wx[m] < W and p[1] + \
                        wy[n] >= 0 and p[1] + wy[n] < H:
                    if S[p[1] + wy[n]][p[0] + wx[m]
                                       ] == '.' and dp[p[1] + wy[n]][p[0] + wx[m]] == -1:
                        ds.append([p[0] + wx[m], p[1] + wy[n]])
                        dp[p[1] + wy[n]][p[0] + wx[m]] = cost + 1
                        check = True
    if check:
        c += 1
    else:
        break

print(dp[Dh - 1][Dw - 1])