import queue

def bfs(x, y):
    if M[x][y] == c:
        return False
    else:
        M[x][y] = c
    q = queue.Queue()
    q.put([x,y])
    while not q.empty():
        now = q.get()
        nowx = now[0]
        nowy = now[1]
        if nowx ==  D1 - 1 and nowy == D2 -1:
            return True
        if 0 < nowy and m[nowx][nowy -1] == '.' and M[nowx][nowy -1] == -1:
            M[nowx][nowy -1] = c
            q.put([nowx,nowy-1])
        if H-1 > nowy and m[nowx][nowy +1] == '.' and M[nowx][nowy +1] == -1:
            M[nowx][nowy +1] = c
            q.put([nowx,nowy+1])
        if 0 < nowx and m[nowx - 1][nowy] == '.'and M[nowx - 1][nowy] == -1:
            M[nowx - 1][nowy] = c
            q.put([nowx - 1,nowy])
        if W - 1 > nowx and m[nowx + 1][nowy] == '.'and M[nowx + 1][nowy] == -1:
            M[nowx + 1][nowy] = c
            q.put([nowx + 1,nowy])
    return False



def jump():
    for i in range (H):
        for j in range (W):
            if M[i][j] == c:
                for k in range(max(0,i-2),min(H,i+3)):
                    for l in range(max(0,j-2),min(W,j+3)):
                        if M[k][l] == -1 and m[k][l] == '.':
                            Q.put([k,l])




H,W = map(int,input().split())
C1,C2 =map(int,input().split())
D1,D2 = map(int,input().split())

m = []
for i in range (H):
    m.append(list(input()))



M = [[-1 for i in range (W)] for j in range (H)]
c = 0
Q = queue.Queue()

Q.put([C1-1,C2-1])
flag = False
f2 = False
while True:
    if Q.empty():
        print(-1)
        f2 = True
        break
    while not Q.empty():
        x,y = Q.get()
        if bfs(x,y) == True:
            flag = True
            break
    if flag == True:
        break
    else:
        jump()
        c += 1

if not f2:
    print(c)


### FIXED CODE ###

from collections import deque


def main():
    from sys import stdin
    readline = stdin.readline

    from builtins import max, min, range

    INF = 10 ** 6

    H, W = map(int, readline().split())
    Ch, Cw = map(lambda x: int(x) - 1, readline().split())
    Dh, Dw = map(lambda x: int(x) - 1, readline().split())
    S = [readline()[:-1] for _ in range(H)]

    t = [[INF] * W for _ in range(H)]
    for h in range(H):
        th = t[h]
        Sh = S[h]
        for w in range(W):
            if Sh[w] == '#':
                th[w] = -1

    t[Ch][Cw] = 0
    q = deque([(Ch, Cw)])
    a = 0
    while q:
        warpq = []
        while q:
            h, w = q.popleft()
            warpq.append((h, w))
            if h - 1 >= 0 and t[h - 1][w] > a:
                q.append((h - 1, w))
                t[h - 1][w] = a
            if h + 1 < H and t[h + 1][w] > a:
                q.append((h + 1, w))
                t[h + 1][w] = a
            if w - 1 >= 0 and t[h][w - 1] > a:
                q.append((h, w - 1))
                t[h][w - 1] = a
            if w + 1 < W and t[h][w + 1] > a:
                q.append((h, w + 1))
                t[h][w + 1] = a

        if t[Dh][Dw] != INF:
            break

        a += 1
        for h, w in warpq:
            for i in range(max(0, h - 2), min(H, h + 3)):
                ti = t[i]
                for j in range(max(0, w - 2), min(W, w + 3)):
                    if ti[j] > a:
                        ti[j] = a
                        q.append((i, j))

    if t[Dh][Dw] == INF:
        print(-1)
    else:
        print(t[Dh][Dw])


main()