### BUGGY CODE ###

import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

H, W = MI()
Ch, Cw = MI()
Dh, Dw = MI()
S_ = []
for i in range(H):
    s = S()
    S_.append(s)

# i行目j列目のマスに(i - 1) * W + j　- 1の数字を割り振る
star = (Ch - 1) * W + Cw - 1
goal = (Dh - 1) * W + Dw - 1
g = [[] for _ in range(H * W)]
for i in range(H * W):
    h, w = divmod(i, W)
    if S_[h][w] == '#':
        continue
    else:
        if w != 0 and S_[h][w - 1] == '.':
            g[i].append(i - 1)
        if w != W - 1 and S_[h][w + 1] == '.':
            g[i].append(i + 1)
        if h != 0 and S_[h - 1][w] == '.':
            g[i].append(i - W)
        if h != (H - 1) and S_[h + 1][w] == '.':
            g[i].append(i + W)


from collections import deque

def bfs(u, n):
    queue = deque([u])
    d = [None] * n # uからの距離の初期化
    d[u] = 0 # 自分との距離は0
    dif_list = [u]
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = 0
                queue.append(i)
                dif_list.append(i)
    return dif_list

def warp(A, B):
    dif_A = []
    bfs_list = []
    for a in A:
        h, w = divmod(a, W)
        left = min(w, 2)
        right = min(W - 1 - w, 2)
        a_list = []
        for i in range(-left, right + 1):
            for j in range(-2, 3):
                a_ = a + i + (j * W)
                a_list.append(a_)

        for a_ in a_list:
            h_, w_ = divmod(a_, W)
            if 0 <= a_ <= H * W - 1 and a_ not in B and a_ not in dif_A and S_[h_][w_] == '.':
                dif_A.append(a_)
    for b in dif_A:
        if b not in bfs_list:
            for c in bfs(b, H * W):
                if c not in dif_A:
                    bfs_list.append(c)
                    dif_A.append(c)
    return dif_A

dif_0 = bfs(star, H * W)
dif_total = dif_0
ans = 0
if goal in dif_0:
    print(0)
    exit()
else:
    while dif_0 != []:
        dif_0 = warp(dif_0, dif_total)
        dif_total += dif_0
        ans += 1
        if goal in dif_0:
            print(ans)
            exit()
    print(-1)




### FIXED CODE ###

import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    from collections import deque
    H, W = MI()
    C = LI()
    D = LI()
    C = [i - 1 for i in C]
    D = [i - 1 for i in D]
    S_ = []
    for i in range(H):
        s = S()
        S_.append(s)
    cost = [[10 ** 10] * W for _ in range(H)]
    cost[C[0]][C[1]] = 0
    queue = deque()
    queue.append(C)
    while len(queue) != 0:
        a = queue.popleft()
        a_cost = cost[a[0]][a[1]]
        for i in range(-2, 3):
            if a[0] + i < 0 or a[0] + i >= H:
                continue
            for j in range(-2, 3):
                if a[1] + j < 0 or a[1] + j >= W:
                    continue
                if i == j == 0:
                    continue
                b = [a[0] + i, a[1] + j]
                b_cost = cost[b[0]][b[1]]
                if S_[b[0]][b[1]] == '#':
                    continue
                if (abs(i) == 1 and j == 0) or (abs(j) == 1 and i == 0):
                    if a_cost < b_cost:
                        cost[b[0]][b[1]] = a_cost
                        queue.appendleft(b)
                else:
                    if a_cost + 1 < b_cost:
                        cost[b[0]][b[1]] = a_cost + 1
                        queue.append(b)
    ans = cost[D[0]][D[1]]
    if ans != 10 ** 10:
        print(ans)
    else:
        print(-1)

if __name__ == "__main__":
    main()