### BUGGY CODE ###

import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    H, W = NMI()
    Ch, Cw = NMI()
    Dh, Dw = NMI()
    S_ = []
    for i in range(H):
        s = SI()
        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 = []
        dif_A_total = [0] * (H * W)
        bfs_list = []
        for a in A:
            h, w = divmod(a, W)
            left = min(w, 2)
            right = min(W - 1 - w, 2)

            for i in range(-left, right + 1):
                for j in range(-2, 3):
                    a_ = a + i + (j * W)
                    h_, w_ = divmod(a_, W)
                    if 0 <= a_ <= H * W - 1 and B[a_] == 0 and dif_A_total[a_] == 0 and S_[h_][w_] == '.':
                        dif_A_total[a_] = 1
                        B[a_] = 1
                        dif_A.append(a_)
        for b in dif_A:
            if b not in bfs_list:
                b_ = bfs(b, H * W)
                for c in b_:
                    if c not in dif_A:
                        bfs_list.append(c)
                        dif_A.append(c)
                        B[c] = 1
        return dif_A

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

if __name__ == "__main__":
    main()


### 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()