import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
C = np.frombuffer(read(), dtype="S1").reshape(
    H, -1)[:, :W]  # 整形の仕方すごい。一番右の列はいらないから消している。
C = 1 * (C == b'#')  # エレガント


def search_empty(C, i, j, count):
    C[i, j] = count
    # 上
    if j > 0 and C[i, j - 1] == 0:
        search_empty(C, i, j - 1, count)
    # 左
    if i > 0 and C[i - 1, j] == 0:
        search_empty(C, i - 1, j, count)
    # 右
    if i < W - 1 and C[i + 1, j] == 0:
        search_empty(C, i + 1, j, count)
    # 下
    if j < H - 1 and C[i, j + 1] == 0:
        search_empty(C, i, j + 1, count)


count = 2
for i in range(H):
    for j in range(W):
        if C[i, j] == 0:
            search_empty(C, i, j, count)
            count += 1

D = np.zeros((count, count))

for i in range(H):
    for j in range(W):
        if C[i, j] != 1:
            for k in range(-2, 3):
                for l in range(-2, 3):
                    k = i + k
                    l = j + l
                    if k < 0 or k >= H:
                        continue
                    if l < 0 or l >= W:
                        continue
                    if C[k, l] != 1 and C[i, j] != C[k, l]:
                        D[C[i, j], C[k, l]] = 1
                        D[C[k, l], C[i, j]] = 1

start = C[ch-1, cw-1]
end = C[dh-1, dw-1]

count = 0

if start == end:
    print(0)

li = [[start, 0]]
while len(li):
    z, c = li.pop(0)
    if c == max(C.flatten()):
        print(-1)
        break
    for i in range(2, int(max(C.flatten()))+1):
        if i != z and D[z, i] != 0:
            li.append([i, c + 1])
    if c != 0 and end == z:
        print(c)
        break


### FIXED CODE ###

import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
from heapq import *

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


@njit((b1[:, :], i8, i8, i8, i8), cache=True)
def main(wall, Sx, Sy, Tx, Ty):
    INF = 1 << 30
    H, W = wall.shape
    S = Sx * W + Sy
    T = Tx * W + Ty
    dist = np.full(H * W, INF, np.int64)
    q = [(0, S)]
    while q:
        dv, v = heappop(q)
        if dv > dist[v]:
            continue
        vx, vy = divmod(v, W)
        # そのままあるく
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            wx, wy = vx + dx, vy + dy
            if not(0 <= wx < H):
                continue
            if not (0 <= wy < W):
                continue
            if wall[wx, wy]:
                continue
            w = wx * W + wy
            if dist[w] <= dv:
                continue
            dist[w] = dv
            heappush(q, (dv, w))
        # ワープする
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                wx, wy = vx + dx, vy + dy
                if not (0 <= wx < H):
                    continue
                if not (0 <= wy < W):
                    continue
                if wall[wx, wy]:
                    continue
                w = wx * W + wy
                if dist[w] <= dv + 1:
                    continue
                dist[w] = dv + 1
                heappush(q, (dv + 1, w))
    ans = dist[T]
    if ans == INF:
        ans = -1
    return ans


H, W = map(int, readline().split())
Sx, Sy = map(lambda x: int(x) - 1, readline().split())
Tx, Ty = map(lambda x: int(x) - 1, readline().split())
wall = np.frombuffer(read(), 'S1').reshape(H, -1)[:, :W] == b'#'

print(main(wall, Sx, Sy, Tx, Ty))